滚动后 UITableView 行颜色更改

use*_*935 1 uitableview ios

Code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.selectionStyle = UITableViewCellStyleDefault;
}
cell.contentView.backgroundColor = [UIColor clearColor];

cell.textLabel.text = @"testing";
if(indexPath.row == 1){
    cell.textLabel.textColor = UIColorMake(kOrangeColour);
}

return cell;
Run Code Online (Sandbox Code Playgroud)

}

我有一个故事板中有 14 行的 tableview。我更改了 2 行的颜色(即 1 索引)。当我多次上下滚动 tableview 时,我发现 14 行(即 13 索引)颜色也改变了。现在两者第 1 行和第 14 行是橙色。按照我的编码,它应该只更改一行的颜色。为什么会发生这种情况?任何帮助将不胜感激。提前致谢。

Gop*_*ath 5

if(indexPath.row == 1){
    cell.textLabel.textColor = UIColorMake(kOrangeColour);
} else {
    cell.textLabel.textColor = [UIColor whiteColor]; // change is here
}
Run Code Online (Sandbox Code Playgroud)

代替

if(indexPath.row == 1){
    cell.textLabel.textColor = UIColorMake(kOrangeColour);
}
Run Code Online (Sandbox Code Playgroud)

在 的可重用性期间UITableViewCell,您必须声明ifelse