【发布时间】:2022-01-18 20:18:17
【问题描述】:
我有一个表格视图,其中包含 2 个 3 行的原型单元格。第一个原型单元只有一个视图。第二个原型单元有一个按钮。 第一个原型单元格只有 1 行,也是第一个 indexpath.row 第二个原型单元有 2 行。 (第二和第三个 indexpath.row )
我想将我的模型(数组)与第 2 行和第 3 行合并。但是应用程序崩溃并说超出范围。
表格视图类
let modelArray = [phone, remote]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 + modelArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
if indexPath.row < 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CELL1", for:
indexPath) as! CELL1
cell.view.backgroundColor = .red
return cell
}
let cell2 = tableView.dequeueReusableCell(withIdentifier: "CELL2", for: indexPath)
CELL2
这是我得到超出范围错误的地方。 (我也试过没有 + 1 )
cell2.configure(title: modelArray[indexpath.row + 1])
return cell2
}
CELL2 类
@IBOutlet weak var buttonName: UIButton!
private var title: String = ""
func configure(title: String) {
self.title = title
buttonName.setTitle(title, for: .normal)
}
【问题讨论】:
标签: arrays swift uitableview cell