按标签更新UITableView Cell imageView

时间:2013-10-25 08:18:26

标签: ios objective-c uitableview sdk

是否可以通过标签更新iOS UITableView中的单元格imageView?我感谢其他方法可用但我想尽可能使用标签。

我通过以下方式设置图像:

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

在另一种方法中,我可以获得单元格(标签总是唯一的):

NSInteger the_tag = ((UIView*)sender).tag;
UITableViewCell *updateCell = (UITableViewCell*)[tableView viewWithTag:the_tag];

是否可以使用此方法更新单元格?这不起作用:

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

编辑:

经过反思,标签是不好用的方法。您可以使用(如果您有分段表,将为您提供行和部分):

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
    NSLog(@"Tapped Row %@", indexPath);
    UITableViewCell *cell = [self->tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"lunch.png"];
}

4 个答案:

答案 0 :(得分:2)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCell *cell = (CustomCell*)[tblView1 cellForRowAtIndexPath:indexPath.row];

   UIImageView *imageView = cell.imageView;//CustomCell you have imageView as @propertyVariable..
   imageView.image = [UIImage imageNamed:@"image.png"];
}

我希望它会对你有所帮助......

答案 1 :(得分:1)

尝试拨打[updateCell setNeedsLayout][updateCell.imageView setNeedsLayout]

答案 2 :(得分:0)

感谢您的所有答案。经过反思,我觉得标签是一种不好用的方法。我已经用我认为更好的解决方案更新了我原来的帖子。

答案 3 :(得分:0)

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

if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.tag = indexPath.row;
    cell.imageView.image = [UIImage imageNamed:@"icon.png"];
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int tag = 2;//this is same at table cell row number.
UITableViewCell *cell = (UITableViewCell*)[tableView viewWithTag:tag];
cell.imageView.image = [UIImage imageNamed:@"image.png"];
}