我有一个分组的tableview和数据源如下所示:TableView中没有使用正确的TableViewCell
let customCell = UITableViewCell()
customCell.textLabel?.text = "this is a custom cell"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellA", for: indexPath) as! CellA
cell.label.text = "dummy text"
switch indexPath.section {
case 0 :
switch indexPath.row {
case 0 : cell.label.text = "Foo"
case 1 : cell.label.text = "Bar"
default:
fatalError("Row does not exist")
}
case 1 :
switch indexPath.row {
case 0 : return customCell
default:
fatalError("Row does not exist")
}
default:
fatalError("Section does not exist")
}
return cell
}
func numberOfSections(in tableView: UITableView) -> Int { return 2 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0 : return 2
case 1 : return 1
default:
fatalError("Section does not exist")
}
}
问题:
我想第2节使用customCell
而是它仍然使用的电池我使用方法dequeueReusableCell(identifier:,indexPath:)
创建,具有虚拟文本"dummy text"
。如果我使用方法,则不会发生这种情况:dequeueReusableCell(identifier:)
(不含indexPath
)。
这样做的正确方法是什么,或者我应该只使用没有indexPath
的方法?
你有没有调试来检查是否行'情况下0:返回customCell'是否达到? –
你应该在'case 0'和'case 1'设置单元格文本标签数据。 – zsteed
@BenjaminLowry:是的,我已经检查过,它确实到达了'case 0:'。 @zsteed:即使我这样做,出列的单元格将会在'customCell'之上。 –