2015-05-14 49 views
0

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

滚动后的其他细胞也受到影响

enter image description here

这是我的代码

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]; 


} 
+0

因为细胞被重用,你必须设置选择/只要你重用一个没有选择。 – Volker

+0

你如何改变选择颜色? –

+0

UITapGestureRecognizer * singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap :)]; [cell addGestureRecognizer:singleFingerTap]; (void)handleSingleTap:(UITapGestureRecognizer *)识别器TBDCreateGamePlayerCell * cell =(TBDCreateGamePlayerCell *)recognitionizer.view; } – Dev

回答

0

您必须实现此委托方法:

- (void)tableView:(UITableView *)tableView 
    willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath 

在此方法中的变化单元格背景b ack为默认颜色。

Apple Docs

探讨它使用细胞来绘制一排,从而允许委托 定制细胞

表视图将此消息发送到其委托只是 之前显示之前的对象。此方法使 委托人有机会覆盖表视图(例如选择和背景颜色)之前设置的早于 的基于状态的属性。 委托人返回后,表视图仅设置alpha和帧 属性,然后仅在滑动或滑入行时对动画进行动画处理。

0

看一下下面的代码...

-(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]; 

} 

}