OGeek|极客世界-中国程序员成长平台

标题: ios - 访问 heightForRowAtIndexPath 中的自定义单元格属性 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:16
标题: ios - 访问 heightForRowAtIndexPath 中的自定义单元格属性

我有一个自定义 UITableViewCell。我想访问单元格属性,即 UILabel 等。我尝试插入以下代码:

- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
    CategorieCell *customCell = (CategorieCell *)[tableView cellForRowAtIndexPath:indexPath];

    return ...
}

当我运行应用程序时,它崩溃了,但没有提供错误详细信息。问题出在我正在创建的新 customCell 上。还有其他方法可以访问 customCell.m 对象吗?



Best Answer-推荐答案


关于crash,请注意你使用的是cellForRowAtIndexPath:这是你必须实现的UITableViewDatasource中的一个方法,这个方法默认调用heightForRowAtIndexPath,所以它会变成递归的

我假设您希望在此方法中使用自定义单元格,以便从中获取高度。 实现这一点的最佳方法是在 CategorieCell 上编写一个类方法,为您提供包含特定数据的单元格的高度。

其他选项是使用代码提取方法以获取 uitableviewcell,例如

(CategorieCell*) categorieCellForIndexNSIndex)index selectedBOOL)selected{
...
}

关于ios - 访问 heightForRowAtIndexPath 中的自定义单元格属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28355066/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4