![](/img/trans.png)
[英]How to access UITableViewController variable from within a subclass'ed UITableViewCell?
[英]How to access properties on a custom UITableViewCell from a UiTableViewController?
我正在尝试从UitableViewController类访问自定义UiTableViewCell(子类化UITableViewCell)上的label属性。 例如,我有heightForRowAtIndexPath方法,我需要访问标签。
这是我的代码:
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// I need access to the custom UITableView Cell property here
}
有小费吗? 还可以在uiviewcontroller中声明出口并将其链接到自定义uitableviewcell上的标签吗?
谢谢
要从heightForRowAtIndexPath:
访问单元格,请检查以下内容:
如果在初始化后需要访问单元格的标签,则需要在自定义UITableViewCell类的标签中添加tag
,然后可以按以下方式访问标签:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
UILabel *cellLabel = (UILabel *)[cell viewWithTag: 1];
}
高度计算应使用数据模型中的数据(而不是视图)完成。 记住,该单元是用数据模型中的数据创建的。
您不能调用[tv cellForRowAtIndexPath:indexPath]
因为这可能导致tableView:cellForRowAtIndexPath:
和此tableView:heightForRowAtIndexPath:
方法之间的无限递归。
您可以尝试直接调用tableView:cellForRowAtIndexPath:
方法来获取单元格,但这可能会有不希望的副作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.