2015-05-14 22 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]; 

} 

}