如何执行自定义单元格的方法

How to execute a method of a Custom Cell

我有一个自定义 Table 视图单元格需要将信息传递给 heightForRowAtIndexPath 方法。

每个单元格必须回答 heightForRowAtIndexPath 方法应该具有的大小。 如何在特定 indexPath 上引用该单元格并执行其 getHeight 方法?

我使用了 [tableView dequeueReusableCellWithIdentifier:@"theCell" forIndexPath:indexPath]; 但出现错误:"warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available." 当我尝试将其出列时。

我应该改变我的方法还是有办法 "ask" 单元格应有的大小?

这是我的自定义单元格 getHeight 方法

-(CGFloat)getHeight{

    if(isCardOpen){
        //Calculate the size it should have based on its sub products
        //returning a test value for validation only
        return 253;
    }

    return 153;

}

这是我的 heightForRowAtIndexPath

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CardCell *cell = (CardCell*)[tableView dequeueReusableCellWithIdentifier:@"theCell" forIndexPath:indexPath];


    return [cell getHeight];


}

你理解错了。单元格不知道它的高度,高度将由委托函数提供给单元格。

您需要某种模型来存储创建布局所需的信息(哪个单元格应位于索引 X + 哪个高度应位于索引 X)。

如果您需要动态高度,您应该阅读一些关于它的博客文章,例如:https://www.raywenderlich.com/129059/self-sizing-table-view-cells

让单元格决定它的高度不是最好的解决方案。

您的代码有问题

[tableView dequeueReusableCellWithIdentifier:@"theCell" forIndexPath:indexPath]

行。当您尝试使可重复使用的单元格出列时,它会尝试确定它之前的高度,因此您在这里有循环。如果您想要尽可能小的解决方法 - 将其更改为

[self cellForRawAtIndexPath]

您需要自动调整单元格大小。为此,将以下行添加到视图控制器的 viewDidLoad():

tableView.estimatedRowHeight = 85.0  // Use your own value here
tableView.rowHeight = UITableViewAutomaticDimension

您还必须确保每个单元格原型都能够明确计算自己的高度。这意味着您必须设置自动布局约束,以便没有垂直歧义。