//第一个子类
class reportCell: UITableViewCell {
@IBOutlet weak var view1: UIView!
@IBOutlet weak var repLabel: UILabel!
}
//第二个子类
class sectionCell: UITableViewCell {
@IBOutlet weak var view2: UIView!
@IBOutlet weak var secLabel: UILabel!
}
//
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if cellType == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellRep", for: indexPath) as! reportCell
cell.repLabel?.text = type[indexPath.row]
cell.view1.backgroundColor = UIColor(red: 25/255, green: 34/255, blue: 40/255, alpha: 1)
return cell
}
if cellType == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellSec", for: indexPath) as! sectionCell
cell.secLabel?.text = type[indexPath.row]
cell.view2.backgroundColor = UIColor(red: 201/255, green: 113/255, blue: 91/255, alpha: 1)
return cell
}
return UITableViewCell()
}
最佳答案
如果您的表格具有静态数量的单元格,您可以将所需序列中的颜色添加到数组中,并从中获取索引indexPath.row。像这样 let color = [UIColor.red, UIColor.blue, UIColor.yellow] cell.backgroundColor = color[indexPath.row]
关于ios - 带有自定义单元格的 TableView。当单元格滚动(双端队列)时,它们会以混合的 UIView 颜色重新出现。我可以在indexPath.row处设置UIView吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53721343/