如何从单元格访问UIImageView
问题描述:
我在tableView的自定义单元格中有一个imageView。我通过使用viewWithTag
来访问cellforRowAtIndexPath
中的imageView。但是我怎样才能在另一个函数中访问imageView?像didSelectRowAtIndexPath
。如何从单元格访问UIImageView
答
您可以使用[tableView cellForRowAtIndexPath:indexPath]
接入小区,然后从细胞可以访问它的使用viewWithTag
子视图(内容)AS-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// access the cell for selected cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// from cell using viewWithTag:image view's tag access imageView
UIImageView *imageView = [cell viewWithTag:CELL_IMAGE_VIEW_TAG];
}
答
声明细胞对给定indexPath
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
然后访问图像查看
cell.imageView
你不应该想。使用数据模型。如果您有自定义单元格,则不应使用标记,请使用属性。 – Wain 2015-04-06 09:35:46
@是什么意思是使用数据模型? – marosoaie 2015-04-06 09:38:26
您有一组数据,告诉您如何配置单元格,即您的数据模型和数据的真实来源。不应该查询单元以获取该信息。 – Wain 2015-04-06 09:44:46