iOS UITableView单元格滚动后加载不正确?

我在视图中有2个UITableView,我只在第2行第5行和第7行添加了UITableViewCellAccessoryDisclosureIndicator。

但滚动第二张表(第一行消失),然后滚动回顶部(哪一行出现),第一行现在有UITableViewCellAccessoryDisclosureIndicator ?! 第1行莫名其妙地变成了第5行还是第7行? 以下是我的代码cellForRowAtIndexPath:

- (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] autorelease]; } cell.textLabel.textColor = [UIColor blueColor]; cell.detailTextLabel.textColor = [UIColor blackColor]; if (tableView == table1) { cell.textLabel.text = [title1 objectAtIndex:indexPath.row]; cell.detailTextLabel.text = [list1 objectAtIndex:indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } else if (tableView == table2) { cell.textLabel.text = [title2 objectAtIndex:indexPath.row]; cell.detailTextLabel.text = [list2 objectAtIndex:indexPath.row]; if (indexPath.row == 5 || indexPath.row == 7) { cell.selectionStyle = UITableViewCellSelectionStyleBlue; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } else { cell.selectionStyle = UITableViewCellSelectionStyleNone; } } return cell; } 

非常感谢!

UITableViewCells被重用来优化性能。 这发生在[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 你需要在tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath每次调用时明确地设置你希望在单元格上的任何属性tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

像这样的东西应该解决这个问题:

 if (indexPath.row == 5 || indexPath.row == 7) { cell.selectionStyle = UITableViewCellSelectionStyleBlue; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } else { cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryNone; }