2013-10-25 53 views
2

是否有可能通过标签更新iOS UITableView中的cell imageView?我赞赏其他方法可用,但我想尽可能使用标签。通过标签更新UITableView Cell 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"]; 
} 
+1

为什么你更喜欢使用标签?它很脆弱,在这种情况下它破坏了封装。 – Abizern

+0

我认为你需要放置[self.tableView beginUpdates];在你的代码和[self.tableView endUpdates]之前;在代码后面,强制TableView更新其内容。 –

+0

我同意,标签是一个不好的方法!更新了我的答案。 – Colin

回答

1

尝试调用[updateCell setNeedsLayout][updateCell.imageView setNeedsLayout]

+0

谢谢你的回复。不幸的是,他们导致“NSInvalidArgumentException”,原因:' - [UIImageView imageView]:无法识别的选择器发送到实例“错误一如既往。 – Colin

+0

根据你的日志,这听起来像viewWithTag直接返回ImageView,而不是单元格 – slecorne

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"]; 
} 

我希望这将是对你有帮助.. 。

0

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

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"]; 
}