table 视图单元格中的图像随机更改

Image changed randomly in table view cell

我想更改 UITableViewCell 中的特定 UIImageView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView beginUpdates];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UYLTextCell *textCell = (UYLTextCell *)cell;
    textCell.selected = YES;
    //get and show the image at selected cell
    textCell.testImage.image = [UIImage imageNamed:@"image280280.jpg"];
    //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

但是发生的事情是一些单元格有他们的图像视图,即使它没有被点击。

Table 观看重复使用单元格。当一个单元格滚动到屏幕外时,它被添加到一个队列中,并将被重新用于下一个要滚动到屏幕上的单元格。这意味着您在 tableView:cellForRowAtIndexPath: 中的单元格配置代码应该足以正确配置任何单元格,包括之前在不同 indexPath 中选择的单元格。在 tableView:didSelectRowAtIndexPath: 中,将选定的索引路径存储在 属性 中,并使用该 属性 在 tableView:cellForRowAtIndexPath: 中配置您的单元格。无论是否选择单元格,都需要在 tableView:cellForRowAtIndexPath: 中设置图像(除非您在自定义单元格中实施 prepareForReuse)。