我有一个应用程序,在该应用程序中,我使用cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_gray_with_line.png"]];更改了选定单元格的颜色。但是,当我在didselect中选择后执行此操作时,在按下后,不会将我的背景更改回以前的视图
if (indexPath != nil) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} 当弹回这个视图控制器时,我需要和平常一样的背景,而不需要重新加载表视图。有人能帮我吗?
发布于 2013-03-05 14:47:26
您应该为UITableViewCell设置两个属性。
另外,您应该将cell.selectionStyle设置为UITableViewCellSelectionStyleNone。
发布于 2013-03-05 14:52:29
如果UITableViewControllers的clearsSelectionOnViewWillAppear属性设置为YES,则取消选择viewWillAppear上的选定行。如果您使用的不是具有该设置的表视图控制器(我认为,默认设置为YES ),请自己执行此操作:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}实现did (或will)取消选择委托方法并修复您的颜色:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// do what you want to the cell
}顺便说一句,其他人建议在单元格的内容视图(不是cell.backgroundColor,而是cell.contentView.backgroundColor)上设置颜色,并在willDisplayCell:forRowAtIndexPath:中这样做。
发布于 2013-03-05 14:50:38
这应该可以,
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow] ].backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"normal.png"]];
}https://stackoverflow.com/questions/15217393
复制相似问题