ios - swift 3 : UITableView row height changes due to UINavigationViewController

标签 ios swift uitableview uinavigationcontroller

我有一个嵌入到 UINavigationController 中的 UIViewController。我的 UIViewController 包含一个 UITableView,它有两种类型的单元格:单元格 1(第一个单元格)的高度 = 250,单元格 2 的高度 = 85。开始时一切正常但是当我使用 self.navigationController?.pushViewController(vc, animated: true) 粘贴一个单元格以显示其详细信息时,然后当我返回到 parentViewController (包含UITableView) cell1 获得了 cell2 的高度。在 Storyboard 中,我将表格的 rowHeight 设置为 250,对于 cell1 的 View 为 250,对于 cell2 的 View ,我有 85。然后在 Controller 中,我将 table.rowHeight 设置为函数index.row 如您在上面的代码中所见:

// MARK: - Table view

    private func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 if indexPath.row == 0 {
            tableView.rowHeight = 260
            var cell = tableView.dequeueReusableCell(withIdentifier: "homeFirstTableViewCell") as? HomeFirstTableViewCell
//set the content of the cell
} else {
            tableView.rowHeight = 85
            var cell = tableView.dequeueReusableCell(withIdentifier: "homeTableViewCell") as? HomeTableViewCell
}
}

注意:

我只有 iOS 8 设备有这个问题

编辑:

当我尝试这样做时:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if(indexPath.row == 0 ){
            return 260
        }

        return 85
    }

类型为 cell2 的第一个单元格的高度为 cell1

最佳答案

您需要在 Storyboard或 xib 中设置单元格内容的约束(如果您在其中创建它们),一种方法是创建一个 containerView 并为其分配高度约束,并使用这个方法

tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat

而不是这个

tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

并返回一个介于 85 和 250 之间的抽象数字

关于ios - swift 3 : UITableView row height changes due to UINavigationViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40846513/

相关文章:

ios - 键盘到导航栏的高度

ios - 通过 becomeFirstResponder 将焦点分配给 UISearchController 的 UISearchBar 时,键盘不出现

ios - 如何解决来自 Firebase 的 Apple Mach-O 链接器错误?

php - Swift 上传多张图片到 PHP 服务器

swift - 使用 UIFocusGuide 在 tvOS 中进行对角线聚焦

ios - 类似于 Whatsapp 的已读状态的双复选标记

ios - TableViewController/自定义单元格 - 无法移动到下一个文本字段

swift - 表格 View 中的标题

ios - 在自定义单元格中实现倒计时器

ios - 在 Xcode 机器人上运行单元测试后生成 .gcda 文件