【发布时间】:2020-07-02 20:07:18
【问题描述】:
我有 UITableView,默认情况下显示 2 个项目。当我将手机切换到横向模式时,我需要重新渲染所有 tableview 行,然后在行中显示 3 个项目。我已经有了渲染 2 或 3 个项目的逻辑以及我需要的行数,但我不知道如何传递 isLandscaped 布尔值,告诉单元格如何渲染(横向或纵向)。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! BookshelfCell
cell.changeOrientation(orientation: isLandscaped) // doesn't work
cell.isLandscaped = self.isLandscaped // doesn't work
if isLandscaped {
cell.book1 = books[indexPath.row * 3] // Return for left side
if (indexPath.row * 3 + 1) < books.count {
cell.book2 = books[indexPath.row * 3 + 1] // Return for middle
}
if (indexPath.row * 3 + 2) < books.count {
cell.book3 = books[indexPath.row * 3 + 2] // Return for right side
}
} else { // portrait
cell.book1 = books[indexPath.row * 2] // Return for left side
if (indexPath.row * 2 + 1) < books.count {
cell.book2 = books[indexPath.row * 2 + 1] // return for right side
}
}
return cell
}
在 ViewController 中更改 isLandscaped book 的功能(工作正常):
@objc func rotated() {
if UIDevice.current.orientation.isLandscape {
isLandscaped = true
}
if UIDevice.current.orientation.isPortrait {
isLandscaped = false
}
tableView.reloadData()
}
我尝试了协议委托模式,我再次能够从 VC 向单元格发送值,但无法使用 3 个项目重新渲染 tableview。我认为问题在于我总是将isLandscaped bool 发送到现有单元格,但我没有用新值渲染新单元格。如果我默认在单元格中设置 3 个项目(isLandscaped = true),它工作正常。也许有可能以某种方式初始化单元格?我还尝试在任何地方打电话给cell.reloadInputViews() 和tableview.reloadData()。也没什么。
我需要使用 uitableview,它应该可以在 iOS 9.0 上运行,这是这个任务的规则。 如果有人有时间提供帮助,我将不胜感激。如果需要,我可以发送更多代码。
【问题讨论】:
标签: ios swift uitableview uikit parameter-passing