您的问题是您没有考虑方法 1 中的单元重用(您甚至不应该使用方法 2)。执行 if 语句后,您的单元格将变为棕色,并且当该单元格被重用时,无论它位于什么 indexPath.row 中,它仍然是棕色的。每当您像您一样根据 indexPath 设置单元格的某些属性时这样做,您需要有一个 else 子句,而不仅仅是一个 if。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (indexPath.row == 0)
{
cell.backgroundColor = [UIColor brownColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}else{
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
}
NSArray * array = [[list objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = [array valueForKey:@"Number"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"row: %d", indexPath.row];
return cell;
}
只要您坚持方法 1,那么 prepareForSegue 应该可以正常工作。