0

我在 UITableView 的单元格中有数据和图像。我想更改特定单元格的图像,每个单元格都有一个代码。

我无法找到迭代单元格的表格视图的方法。

这是我的 tableview 功能的快照。

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellText = nil;

    NSArray *keys = [[[self packageItems] allKeys] 
                     sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    cellText = [keys objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:CellIdentifier];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    cell.imageView.image=[UIImage imageNamed:@"checkbox.png"];
    //cell.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];

    return cell;
}

还有另一个功能,我想将单元格图像更改为 cell.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];

- (void)OnAction
{
    static NSString *CellIdentifier = @"Cell";
    NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath ];
    cell.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];

}
4

1 回答 1

0

如果您不想使用功能更改表格视图中的某些内容,则不必创建新单元格,只需修改实际单元格即可。只需在您的 cellForRowAtIndexPath 委托中创建条件:

if(indexPath == indexToModify) {
    cell.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];
} else {
    cell.imageView.image=[UIImage imageNamed:@"checkbox.png"];
}

最后在您的 - (void)OnAction 方法中使用:

[self.tableView reloadData];
于 2012-11-01T13:27:16.493 回答