简体   繁体   English

使用indexPath.row更改单元格图像

[英]Change cell image with it's indexPath.row

Currently I'm using: 目前,我正在使用:

cell.imageView.image = [UIImage imageNamed:@"image.png"];

to change the image of the cell. 更改单元格的图像。

But how can I change it if I only know the indexPath.row? 但是,如果我只知道indexPath.row,该如何更改?

I have tried: 我努力了:

[self.tableView cellForRowAtIndexPath:0].imageView.image = [UIImage imageNamed:@"image.png"];

try 尝试

  UITableVieCell *cell = [self.tableView cellForRowAtIndexPath:0];
  cell.imageView.image = ...

What exactly is your question? 您到底是什么问题? How can i change it if i only know the indexpath.row? This question doesnt make sense. 这个问题没有道理。 All you need to change a certain cell is the indexPath . 更改特定单元格所需的全部就是indexPath

UITableVieCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"myImage"];

This code is just a bit different from @JMD. 这段代码与@JMD有点不同。 His will only change the image for the very first cell. 他只会更改第一个单元的图像。 If you want every cell, cellForRowAtIndexPath will get called for each newly created cell. 如果需要每个单元格,将为每个新创建的单元cellForRowAtIndexPath Therefore, inserting indexPath.row instead of 0 should do the trick. 因此,插入indexPath.row而不是0应该可以解决问题。

你也可以这样

((UITableViewCell *)[self.tableView cellForRowAtIndexPath:0]).imageView.image = [UIImage imageNamed:@"image.png"];

You should write your cellForRowAtIndexPath so that it can determine which image to show, and call 您应该编写cellForRowAtIndexPath,以便它可以确定要显示的图像,然后调用

[self.tableView reloadRowsAtIndexPaths:withRowAnimation:]

rather than calling cellForRowAtIndexPath directly. 而不是直接调用cellForRowAtIndexPath You should never call this method directly, as changes will not be permanent if you are using queued up cells. 永远不要直接调用此方法,因为如果使用排队的单元格,更改将不会是永久的。 Make your cellForRowAtIndexPath method smart enough to determine what to show, and reload the row at the indexPath. 使您的cellForRowAtIndexPath方法足够智能,以确定要显示的内容,然后在indexPath处重新加载行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM