ios - 将表中的特定行从 void 更改为空

标签 ios uitableview

我有一个已填充的 TableView,我想在其中更改图像。当我在 cellForRowAtIndexPath 内部时,我可以使用以下方法更改它:

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

但我不知道如何在虚空中做同样的事情,我已经尝试过:

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

但它根本不会改变图像。

谢谢, /DS丹尼索

编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_row = indexPath.row;
[self changeImage];
}

- (void)changeImage{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_row inSection:0]];
if(cell)
{
cell.imageView.image = [UIImage imageNamed:@"image.png"];

} else {
NSLog( @"why is cell null?  Did I set row properly?");
}
}

最佳答案

只需将单元格传递给方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    _row = indexPath.row;
    [self changeImageForCell:cell];
    }

    - (void)changeImageForCell:(UITableViewCell*)cell{
    cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }

关于ios - 将表中的特定行从 void 更改为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16639037/

相关文章:

ios - UITableViewCell 内的 UITextView 键盘问题

ios - NSDateFormatter 日期格式字符串

javascript - 与 UIWebview 表单弹出窗口通信?

ios - 如何将手机图库中的图片添加到 UITableView

ios - UITableViewStyleGrouped 仅删除标题的分隔符

ios - 文本到语音转换

ios - 以编程方式约束 UITextField 会导致错误 "Unable to activate constraint because they have no common ancestor."

ios - 字典中的单元格标题

swift - 在表格 View 中选择行,删除自定义单元格

ios - VC 之间的 Swift Segue,无需链接