2

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

我试过这个

    UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]

但这会崩溃,因为单元格是自定义的。

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

4

3 回答 3

9

而不是使用

UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]

尝试使用

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

希望能帮助到你

于 2012-04-02T12:46:18.550 回答
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];
于 2012-04-02T12:43:34.010 回答
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 的默认显示指示器而不是图像。

于 2012-04-02T12:40:21.300 回答