我有一个嵌入UINavigationController
的UIViewController。我的UIViewController
包含一个UITableView
,其中包含两种类型的单元格:cell1(第一个单元格)的高度为250,而cell2的高度为85.一开始工作正常,但是当我粘贴一个单元格时使用self.navigationController?.pushViewController(vc, animated: true)
显示其详细信息,然后当我返回parentViewController
(包含UITableView
的那个)时,cell1获得了cell2的高度。在故事板中,我将表的rowHeight设置为250,对于cell1 250的视图和cell2,我有85.然后在控制器中我将table.rowHeight
设置为{{1}的函数你可以在上面的代码中看到:
index.row
注意:
我只在iOS 8设备上遇到此问题
修改
当我尝试这个时:
// 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
}
}
第一个具有cell2类型的单元格获得了cell1的高度
答案 0 :(得分:0)
您需要在storyboard或xib中设置单元格内容的约束(如果您在一个中创建它们),执行此操作的一种方法是创建containerView
并为其指定高度约束,以及使用这种方法
tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
而不是
tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
并返回85到250之间的抽象数字