我有一个tableview
,我做了cell.xib
。在这些tableview
会有不同cell
s表示我做的一样xib
。
我如何使用它的一个例子。
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellOne.identifier,
for: indexPath) as? TableViewCellOne
return cell ?? UITableViewCell()
}
if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellTwo.identifier,
for: indexPath) as? TableViewCellTWo
return cell ?? UITableViewCell()
}
return UITableViewCell()
}
但是我不喜欢这种方法。我该怎么做case
?
您可以使用切换用例来做到这一点,如下所示:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: //TableViewCellOne
return 4
case 1: //TableViewCellTwo
return 5
default:
return 0
}
}
然后cellforRow
方法将是:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0: //cell One
let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellOne.identifier,
for: indexPath) as? TableViewCellOne
return cell ?? UITableViewCell()
case 1: //cell two
let cell = tableView.dequeueReusableCell( withIdentifier: TableViewCellTwo.identifier,
for: indexPath) as? TableViewCellTWo
return cell ?? UITableViewCell()
default:
return UITableViewCell()
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句