在选择tableviewcell时,我正在更改tableviewcell的颜色。当我选择tableViewCell上的单元格并滚动时,其他一些单元格也被选中。

滚动后,另一个单元格也会受到影响

这是我的代码
static NSString *identifier = @"TBDCreateGamePlayerCell";
TBDCreateGamePlayerCell *playerCell = (TBDCreateGamePlayerCell *)[tableView dequeueReusableCellWithIdentifier:@"TBDCreateGamePlayerCell"];
if (!playerCell) {
NSLog(@"creating a new cell : %d",row);
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TBDCreateGamePlayerCell" owner:nil options:nil];
playerCell = [nib objectAtIndex:0];
}发布于 2015-05-14 15:48:27
您必须实现此委托方法:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath在此方法中,将单元格背景更改回默认颜色。
讨论
表视图在使用单元格绘制行之前将此消息发送给其代理,从而允许代理在显示单元格对象之前对其进行自定义。此方法使委托有机会重写先前由表视图设置的基于状态的属性,如选择和背景色。委托返回后,表视图仅设置alpha和frame属性,并且仅在滑入或滑出时设置行的动画。
发布于 2015-05-14 17:37:01
请看下面的代码。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selected_index= indexPath.row;
[tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType=UITableViewCellAccessoryNone;
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
if(indexPath.row==selected_index)
{
cell.contentView.backgroundColor = [UIColor whiteColor];
}
else
{
cell.contentView.backgroundColor = [UIColor clearColor];
}
}https://stackoverflow.com/questions/30231916
复制相似问题