滚动closures屏幕时,UITableViewCellAccessory消失

我有一个UITableView充满了对象。 在didSelectRowAtIndexPath方法中,我有一个UITableViewCellAccessoryCheckmark当行被选中时出现,并在未选中时消失。

下面是didSelectRowAtIndexPath方法的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath]; if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) { [curCell setAccessoryType:UITableViewCellAccessoryNone]; compareCount = (compareCount - 1); if (tableView == [[self searchDisplayController] searchResultsTableView]) { NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]]; [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; } else { [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]]; [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]]; } } else { [curCell setAccessoryType:UITableViewCellAccessoryCheckmark]; compareCount = (compareCount + 1); if (tableView == [[self searchDisplayController] searchResultsTableView]) { NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]]; [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; } else { [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]]; [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]]; } } if (compareCount > 0) { if (compareOn == YES){ } else { compareButton.enabled = YES; UIImage *image = [UIImage imageNamed:@"redbutton.png"]; [compareButton setImage:image]; } } else { compareButton.enabled = NO; [compareButton setImage:nil]; [compareButton setCustomView:nil]; } } 

我也有这个作为我的cellForIndexPath

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; for (id currentObject in topLevelObjects){ if ([currentObject isKindOfClass:[UITableViewCell class]]){ cell = (CustomCell *) currentObject; break; } } } // Setting separate tables correctly.... return cell; } 

我的问题是,当被选中的单元格滚动到视图外时,与该值相关联的复选标记现在消失。

我应该怎么做才能保持对号消失?

谢谢

当你滚动数据时,你的单元格会被重新使用(这就是dequeueReusableCellWithIdentifier所做的)。 您在didSelectRowAtIndexPath中获得复选标记的单元格将被回收用于其他行,并且不再与已检查的行有任何连接。

您需要在cellForRowAtIndexPath中设置/取消设置附件视图,以便当选中的行滚动回查看时,它们会得到适当的检查。

对于任何想要更通用的方法和多个选定单元的人来说

首先创build一个(NSMutableArray *)selectedCells来跟踪选定的单元格。 接下来实现2个委托方法

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //add the checkmark to cell when selected if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone){ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; } //once selected add that selected cell to the selectedCells array [self.selectedCells addObject:@(indexPath.row) ]; } 

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ //set the accessory type back to none if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; } //remove the selected cells index from the selectedCells array [self.selectedCells removeObject:@(indexPath.row) ]; } 

现在,当您select一个单元格时,会添加一个复选标记,并将该indexPath.row存储到一个NSMutableArray中。 当您select该单元格时,将删除复选标记并将其从数组中移除。 这意味着数组只会保存被检查的单元格。

然后,我们使用该数组为单元格cellForRowAtIndexPath方法中正确的accessoryType。 每次tableView需要一个单元格时,我们都会调用这个方法,我们告诉它,在创build或者不创build时,销售需要有一个复选标记。

 -(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]; //Create an NSNumber to hold the row of the cell to be created NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row]; //then ask the array if the selectedCells array has that object. //if it does then that cell needs a checkmark when created. if ([self.selectedCells containsObject:rowNsNum]){ cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } [cell.textLabel setText:@"your contents"]; } return cell; } 

UITableViewCell在您滚动时被回收。 所以,当你再次向下和向上滚动tableView时,你看到的单元格可能与先前可见的单元格不同。

您需要每次在cellForRowAtIndexPath中重置单元格的状态。 你的代码有这个评论区域//正确设置单独的表格….

这是你设置的地方。 当这个被调用时,检查单元是否应该显示一个复选标记,并相应地设置它

尝试将所选索引存储在数组或字典中用于didSelectRow方法中的所选单元格,并在cellForRowAtIndexPath中检查该索引是否在数组中可用,然后使用设置accesoryType复选单元格。 希望你能理解

这是我发现最好的工作:

(只是“添加此”和“结束添加”之间的任何代码)

此外,请确保将“(您的行数)”更改为返回int的对象,或者是一个int本身。 如myCustomArray.count或24。

 //In .h file add this NSMutableArray *indexArray; //in .m file - (void)viewDidLoad { //Add this indexArray = [[NSMutableArray alloc] init]; int i; for (i = 0; i <= (Your Number ofRows); i++) { [indexArray addObject:[NSNumber numberWithDouble:0]]; } NSLog(@"indexArray = %@",indexArray);//You can check the console now to see if you have an array of "zeros" //End Add this } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; UITableViewCell *curCell = [beerTable cellForRowAtIndexPath:indexPath]; if (curCell.accessoryType == UITableViewCellAccessoryCheckmark) { [curCell setAccessoryType:UITableViewCellAccessoryNone]; compareCount = (compareCount - 1); //Add this [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:0]]; //End Add this if (tableView == [[self searchDisplayController] searchResultsTableView]) { NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; [compareBeers removeObject:[searchResults objectAtIndex:indexPath.row]]; [compareCarbs removeObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; } else { [compareBeers removeObject:[beerNames objectAtIndex:indexPath.row]]; [compareCarbs removeObject:[carbAmount objectAtIndex:indexPath.row]]; } } else { [curCell setAccessoryType:UITableViewCellAccessoryCheckmark]; compareCount = (compareCount + 1); //Add this [indexArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithDouble:1]]; //End Add this if (tableView == [[self searchDisplayController] searchResultsTableView]) { NSString *objBeer = [searchResults objectAtIndex:indexPath.row]; [compareBeers addObject:[searchResults objectAtIndex:indexPath.row]]; [compareCarbs addObject:[carbAmount objectAtIndex:[beerNames indexOfObject:objBeer]]]; } else { [compareBeers addObject:[beerNames objectAtIndex:indexPath.row]]; [compareCarbs addObject:[carbAmount objectAtIndex:indexPath.row]]; } } if (compareCount > 0) { if (compareOn == YES){ } else { compareButton.enabled = YES; UIImage *image = [UIImage imageNamed:@"redbutton.png"]; [compareButton setImage:image]; } } else { compareButton.enabled = NO; [compareButton setImage:nil]; [compareButton setCustomView:nil]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; for (id currentObject in topLevelObjects){ if ([currentObject isKindOfClass:[UITableViewCell class]]){ cell = (CustomCell *) currentObject; break; } } } // Setting separate tables correctly.... //Add this if ([[indexArray objectAtIndex:indexPath.row] doubleValue] == 1) { cell.accesoryType = UITableViewCellAccesoryTypeCheckmark; } else { cell.accesoryType = UITableViewCellAccesoryTypeNone; } //End Add this return cell; }