我在viewForHeaderInSection函数中遇到一个问题。在ios 10中运行良好,但在ios 11中,每次重新加载tableview时都会创建重复视图,这意味着dequeueReusableHeaderFooterView不工作。
我遵循添加viewForHeaderInSection的过程。
首先,我为自定义头视图注册nib,该视图是UITableViewHeaderFooterView的子类

 self.tblView.register(UINib(nibName: "CommentShotringHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: "CommentShotringHeader")

我的UITableViewDelegate方法是
 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 45.0
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let viewHeader = self.tblView.dequeueReusableHeaderFooterView(withIdentifier: "CommentShotringHeader") as! CommentShotringHeader
    return viewHeader
}

这是一个类似于IOS 11的问题,或者我已经搜索过的这个方法中的任何更改,但是我仍然没有得到正确的解决方案。
如果有人也面临同样的问题。找到了解决办法。请推荐我。
提前谢谢。

最佳答案

不要在iOS11上使用estimatedSectionHeaderHeight(测试到11.3)。它可能导致多个问题,其中一个是你所解释的。
如果你能预先计算高度使用
self.tableView.sectionHeaderHeight = 40;
而不是
self.tableView.estimatedSectionHeaderHeight = 40;
否则重写委托方法:

// Swift
public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    // return calculated height
}

// Obj-c
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    // return calculated height
}

关于swift - iOS 11中的Tableview header View 问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47327098/

10-14 22:11
查看更多