0

我正在尝试从 UitableViewController 类访问自定义 UiTableViewCell(子类 UITableViewCell)上的标签属性。例如,我有 heightForRowAtIndexPath 方法,我需要访问标签。

这是我的代码:

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {

   // I need access to the custom UITableView Cell property here

}

有小费吗?甚至可以在 uiviewcontroller 中声明插座并将其链接到自定义 uitableviewcell 上的标签?

谢谢

4

2 回答 2

1

要从 访问单元格heightForRowAtIndexPath:,请检查以下内容:


如果您需要在初始化后访问单元格的标签,那么您需要tag在自定义 UITableViewCell 类中添加标签,您可以通过以下方式访问标签:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
    UILabel *cellLabel = (UILabel *)[cell viewWithTag: 1];
}
于 2012-10-29T03:40:43.273 回答
0

高度计算应使用数据模型中的数据完成,而不是视图。请记住,单元格是使用数据模型中的数据创建的。

您不能调用[tv cellForRowAtIndexPath:indexPath],因为这会导致 thetableView:cellForRowAtIndexPath:和 thistableView:heightForRowAtIndexPath:方法之间的无限递归。

您可以尝试直接调用您的tableView:cellForRowAtIndexPath:方法来获取单元格,但这可能会产生不良副作用。

于 2012-10-29T03:41:51.477 回答