我已经为纵向和横向模式实现了具有不同单元格的tableView。
这里是代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
cell = tableView.dequeueReusableCell(withIdentifier: "LandscapeCustomerTableViewCell", for: indexPath)
tableViewHeader.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 70)
tableViewHeader.isHidden = false
}
else {
cell = tableView.dequeueReusableCell(withIdentifier: "PortraitCustomerTableViewCell", for: indexPath)
tableViewHeader.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
tableViewHeader.isHidden = true
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
return 50
}else {
return 136
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
tableView.reloadData()
}
但是我面临的问题是细胞具有意想不到的行为。
当我滚动单元格时,一切都按预期开始工作。
我正在模拟器上测试该应用。这可能是问题,还是代码中还有其他问题?
在cellForRowAt
中尝试添加
cell.autoresizingMask = .flexibleWidth
如果没有,那么我们需要查看如何用数据填充表。