Uitableview 如果我有自定义单元格,是否可以在HeightForRowatineXpath中调用tableView CellForRowatineXpath tableView来查找单元格的大小?

Uitableview 如果我有自定义单元格,是否可以在HeightForRowatineXpath中调用tableView CellForRowatineXpath tableView来查找单元格的大小?,uitableview,height,cell,xib,Uitableview,Height,Cell,Xib,我可以在HeightForRowatineXpath中调用TableViewCellForRowatineXpath TableView来查找单元格的大小吗 就性能而言,它是否太贵了?如果不是的话,我如何才能找到来自XIB的单元格高度?不要这样做。反正也不行。为所有单元格调用tableView:heightForRowAtIndexPath:。和CellForRowatineXpath:无论如何都不会返回不可见IndExpath的单元格 当tableView:heightForRowAtInde

我可以在HeightForRowatineXpath中调用TableViewCellForRowatineXpath TableView来查找单元格的大小吗


就性能而言,它是否太贵了?如果不是的话,我如何才能找到来自XIB的单元格高度?

不要这样做。反正也不行。为所有单元格调用tableView:heightForRowAtIndexPath:。和CellForRowatineXpath:无论如何都不会返回不可见IndExpath的单元格

当tableView:heightForRowAtIndexPath:在numberOfSectionsInTableView:和tableView:NumberOfRowInSection:之后第一次调用时,根本没有单元格

只需在interface builder中检查单元格的高度。选择UITableViewCell视图并查看尺寸面板

因为tableView:heightforrowatinexpath:是为每个单元格调用的,所以如果只有一个高度,则应该在viewdiload中设置单元格的高度。如果tableView:heightforrowatinexpath:未实现,则不会调用它,如果您有许多单元格,这非常好

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 44.0f;
}
如果您有不同的高度,请从数据源中计算出高度。例如,从数据源NSArray中检查indexPath处的对象,并使用与tableView中相同的逻辑:cellForRowatineXpath:确定要使用哪个单元格。然后返回适当的高度


编辑:我刚刚测试了它,在tableView:heightForRowAtIndexPath:中查询tableView中的一个单元格会导致无限循环

另一种方法是:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static CGFloat cellHeight = 0;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifyer"];
        cellHeight = cell.height;
    });

return cellHeight;
}