动态单元格高度和preferredContentSize

时间:2015-11-13 13:56:09

标签: ios uitableview uipopovercontroller

我正在使用this算法在单元格中使用多行标签。我想在弹出窗口中显示表视图。因此,我将preferredContentSize设置为viewDidAppear中的here

但是如果我查询tableview的contentSize,我得到了错误的高度。尽管我的最后一个单元格具有动态高度(取决于多行标签中的文本),但它似乎始终采用行的默认高度(44f)。

我注意到在计算所有单元格的高度之前调用了viewDidAppear。那是什么意思?以下是heightForRowAtIndexPath中查询的索引路径的摘录:

2015-11-13 14:49:20.592 [53865:2230330] Default: <NSIndexPath: 0x7eb27130> {length = 2, path = 0 - 0}
2015-11-13 14:49:20.594 [53865:2230330] Default: <NSIndexPath: 0x7e904750> {length = 2, path = 0 - 1}
2015-11-13 14:49:20.595 [53865:2230330] Default: <NSIndexPath: 0x7e9f7730> {length = 2, path = 0 - 2}
2015-11-13 14:49:20.596 [53865:2230330] Default: <NSIndexPath: 0x7e9fbd60> {length = 2, path = 0 - 3}
2015-11-13 14:49:20.596 [53865:2230330] Default: <NSIndexPath: 0x7e91bee0> {length = 2, path = 1 - 0}
2015-11-13 14:49:20.597 [53865:2230330] Default: <NSIndexPath: 0x7e90bde0> {length = 2, path = 1 - 1}
2015-11-13 14:49:20.598 [53865:2230330] Default: <NSIndexPath: 0x7e9081e0> {length = 2, path = 1 - 2}
2015-11-13 14:49:20.599 [53865:2230330] Default: <NSIndexPath: 0x7e902d30> {length = 2, path = 1 - 3}
2015-11-13 14:49:20.599 [53865:2230330] Default: <NSIndexPath: 0x7e900b10> {length = 2, path = 1 - 4}
2015-11-13 14:49:20.600 [53865:2230330] Default: <NSIndexPath: 0x7ec24470> {length = 2, path = 0 - 0}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24590> {length = 2, path = 0 - 1}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24570> {length = 2, path = 0 - 2}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24580> {length = 2, path = 0 - 3}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24a60> {length = 2, path = 1 - 0}
2015-11-13 14:49:20.601 [53865:2230330] Default: <NSIndexPath: 0x7ec24bf0> {length = 2, path = 1 - 1}
2015-11-13 14:49:20.602 [53865:2230330] Default: <NSIndexPath: 0x7ec24d80> {length = 2, path = 1 - 2}
2015-11-13 14:49:20.602 [53865:2230330] Default: <NSIndexPath: 0x7ec24f10> {length = 2, path = 1 - 3}
2015-11-13 14:49:20.602 [53865:2230330] Default: <NSIndexPath: 0x7ec250a0> {length = 2, path = 1 - 4}
// viewDidAppear is called setting the preferredContentSize
2015-11-13 14:49:31.374 [53865:2230330] Default: <NSIndexPath: 0x7e80fb20> {length = 2, path = 2 - 0}
2015-11-13 14:49:31.378 [53865:2230330] Adaptive: <NSIndexPath: 0x7e9088b0> {length = 2, path = 3 - 0}
2015-11-13 14:49:31.379 [53865:2230330] Default: <NSIndexPath: 0x7ec231c0> {length = 2, path = 2 - 0}

如何设置正确的preferredContentSize

1 个答案:

答案 0 :(得分:0)

不知道为什么,但似乎estimatedHeightForRowAtIndexPath对此有影响。

如果我得到动态单元格的索引路径,我会计算单元格高度并将其用于估算。我在C#中的代码如下所示:

public override nfloat EstimatedHeight(UITableView tableView, NSIndexPath indexPath)
{
    if (indexPath.Section == 3)
    {
        return this.GetHeightForRow(tableView, indexPath);
    }

    return UITableView.AutomaticDimension;
}

这似乎有效,但我仍然认为这是一种黑客......