我有一个嵌入到 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/