Ios 滚动表格视图时,表格视图单元格背景色被禁用
在我的应用程序中,我有一个tableView,我改变了单元格的背景颜色,当它被选中时,我将代码编写为Ios 滚动表格视图时,表格视图单元格背景色被禁用,ios,uitableview,uibackgroundcolor,Ios,Uitableview,Uibackgroundcolor,在我的应用程序中,我有一个tableView,我改变了单元格的背景颜色,当它被选中时,我将代码编写为 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.backgroundColor = [UIC
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}
问题是当我滚动tableView时,单元格的背景色被禁用,白色不可见意味着背景色效果被移除。表格视图在向外滚动时重用了单元格,因此删除了单元格背景效果。我知道问题出在哪里,但我不知道如何处理这个问题,即使表格视图被滚动,也要将选定单元格的背景色保持为白色。请告诉我此问题的解决方案。更改选定行背景的最佳方法是在创建单元格时更改
selectedBackgorundView
。这样,您就不需要处理didselectrowatinexpath:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"myCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor whiteColor];
cell.selectedBackgroundView = v; // Set a white selected background view.
}
// Set up the cell...
cell.textLabel.text = @"foo";
return cell;
}
这将不起作用,因为单元格会被重用。因此,当单元格重新使用时,您的背景色可能会被覆盖
你应该使用单元格背景视图。正如Pulkit所写的那样。谢谢你的建议,但我只需要在选定单元格时更改单元格的背景色。@smily它只会在选定单元格时更改单元格的背景色。这是因为如果选择了单元格,您将为该单元格分配不同的视图,然后iOS将根据是否选择该单元格来切换视图。谢谢jayvee,但在哪种方法中,必须设置selectedBackgroundView for cell?迪德罗:。。或者是cellForRow:?请向我们展示您的完整cellForRow:实现。那我们就可以帮忙了。