ios - 带有自定义单元格的 TableView。当单元格滚动(双端队列)时,它们会以混合的 UIView 颜色重新出现。我可以在indexPath.row处设置UIView吗?

标签 ios swift uitableview cell

//第一个子类

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/

相关文章:

IOS 启动画面正确呈现然后元素跳到顶部

ios - 如何从自定义 View 单元格重新加载数据?

ios - 在 xcode 中的功能下找不到热点

iphone - 在录制过程中按主页按钮时,红色横幅会突然出现并消失

IOS:致命异常:NSRangeException

swift - NSDate 比较减少了一天

javascript - 当方向更改为横向模式时,如何隐藏 Safari 的工具栏?

swift - 自调整单元格使 View 拉伸(stretch)

swift - 如何在tableview中的不同按钮中播放不同的声音

ios - 更改 tableView 单元格的高度不起作用