【发布时间】:2017-09-24 14:39:41
【问题描述】:
我尝试在 UITableViewCell 上的特定 indexPath.row 处更改单元格的 backgroundColor 和 textLabel。问题是,textLabel 在我指示的 indexPath.row 处正确更改。然而,当我滚动 UITableView 时,一些单元格不仅在那个 indexPath.row 上改变了背景颜色,而且这些不情愿的其他单元格也改变了,这些单元格不在我需要的 indexPath.row 上。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* aCell;
aCell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
if (indexPath.row == 7) {
NSLog(@"enter this specific row");
aCell.textLabel.text = [cellArray objectAtIndex:indexPath.row];
[aCell setBackgroundColor:[UIColor lightGrayColor]];
}
return aCell;
}
我在 indexPath.row 匹配条件上添加了一个 NSLog,控制台只在 indexPath.row 相等时打印出文本。奇怪的是 UITableViewCell 更改背景颜色没有进入条件,因为打印输出对此不起作用。 我假设变量 aCell 在 indexPath 之后无法正常工作。 我的问题是 [aCell setBackgroundColor:[UIColor lightGrayColor]];通过所需的 indexPath.row 正常工作。
有人可以给我一个建议。非常感谢。
【问题讨论】:
标签: ios objective-c uitableview