Ios 为特定单元格设置UITableView单元格类型的更好方法
我使用下面的代码来设置所需的单元格类型,具体取决于indexath。这是一个如此混乱的方法,我相信它可以以某种方式清理。是否可以:Ios 为特定单元格设置UITableView单元格类型的更好方法,ios,swift,uitableview,Ios,Swift,Uitableview,我使用下面的代码来设置所需的单元格类型,具体取决于indexath。这是一个如此混乱的方法,我相信它可以以某种方式清理。是否可以: 使用if语句检查indexPath的多个int值?(例如,如果indexath.row==0或1或2{) 使用if语句仅设置单元格标识符,然后仅在if语句后声明文本标签的值 或者,如果有人对如何使它更实用有任何其他想法,我会非常感激 代码如下: if indexPath.row == 0 { let cell = tableView.d
if
语句检查indexPath的多个int
值?(例如,如果indexath.row==0或1或2{)if
语句后声明文本标签的值 if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
return cell
} else if indexPath.row == 5 {
let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
return cell
} else if indexPath.row == 10 {
let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
return cell
} else if indexPath.row == 14 {
let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
return cell
} else if indexPath.row == 18 {
let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
return cell
} else { //default
let cell = tableView.dequeueReusableCell(withIdentifier: "default") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
cell.nameLabel.text = tableViewData[indexPath.row].name
return cell
}
您可以将
if/else
更改为:
if [0, 5, 10, 14, 18].contains(indexPath.row) {
//timeCell
let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
return cell
} else {
// default
let cell = tableView.dequeueReusableCell(withIdentifier: "default") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
cell.nameLabel.text = tableViewData[indexPath.row].name
return cell
}
或者使用开关
:
switch indexPath.row {
case 0, 5, 10, 14, 18:
//timeCell
default:
// default
}
您可以将
if/else
更改为:
if [0, 5, 10, 14, 18].contains(indexPath.row) {
//timeCell
let cell = tableView.dequeueReusableCell(withIdentifier: "timeCell") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
return cell
} else {
// default
let cell = tableView.dequeueReusableCell(withIdentifier: "default") as! FridayTableCell
cell.dateLabel.text = tableViewData[indexPath.row].time
cell.nameLabel.text = tableViewData[indexPath.row].name
return cell
}
或者使用开关
:
switch indexPath.row {
case 0, 5, 10, 14, 18:
//timeCell
default:
// default
}
正是我所需要的,这么简单,但是工作得很好。我需要的是这么简单,但是工作得很完美。考虑一下使用部分吧。我已经考虑过了,但是我发现行对于我的用例来说是更好的。我不能想象一个区段不会更简单…考虑使用部分。我已经考虑过了,但是我发现行工作得更好。我的用例。我无法想象一个部分会变得更简单。。。