如何访问tableview中的单元格?

时间:2012-04-02 12:27:10

标签: iphone uitableview ios5 xcode4.3 custom-cell

我是iPhone开发的新手。我遇到了问题。我想在我的UITableView中实现一个复选框功能。但我的UITableViewCells是自定义单元格,由图像和3个UILabel组成。我可以将子视图带到表视图中,以便可以放置复选框图像,并且我成功了。但问题来的是每当我点击图像时,只有最后一个单元格复选框被更改。我想访问我点击的单元格。

我试过这个

    UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]

但是由于单元格是自定义的,所以崩溃了。

任何人都可以建议我这样做的好方法吗?

3 个答案:

答案 0 :(得分:9)

而不是使用

UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]

尝试使用

YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];

希望有所帮助

答案 1 :(得分:2)

首先,您需要定义标签 -

#pragma imageViewTag 1

然后在cellForRowAtIndexPath中将此标记分配给图片视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

然后您可以在此标记的帮助下访问您的图像视图 -

 UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];

 UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];

答案 2 :(得分:1)

在TableView中的DidSelect委托方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   UIImageView *image = [cell.contentView viewWithTag:15];
   //You should have set the tag value as 15  when creating the image in cell 
   //and you should have added to the cell contentview

}

如果您无法获得该单元格,那么您可能无法正确使用可重用单元格概念。所以发布你的整个cellforrowindex代码。

如果你正在使用支票那种东西。为什么不使用tableview的默认披露指标而不是图像。

相关问题