简体   繁体   English

如何在同一节Swift 4中根据数组更改UITableViewCell颜色

[英]How Do I change UITableViewCell Colour According to Array in Same Section Swift 4

I have Two Array of Int In which the Index Number is Stored and I want that IndexPath.row cell Background Colour Should Change accordingly. 我有两个存储索引号的Int数组,我希望IndexPath.row单元格的背景色应相应更改。

let   redCell     = ["0","1","4"]
let   greenCell   = ["2","3"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var  cell  = tableView.dequeueReusableCell(withIdentifier: "playQuizTableViewCell") as? playQuizTableViewCell

    if indexPath.row == redCell {
        cell?.textLabel?.backgroundColor = UIColor.red
    } else if indexPath.row == greenCell{
        cell?.textLabel?.backgroundColor = UIColor.green
    } else {
        cell?.textLabel?.backgroundColor = UIColor.black
    }
}

I want to change cell colour of indexPath.row which is matching inside the array. 我想更改与数组内部匹配的indexPath.row的单元格颜色。

Please Guide me. 请指导我。 Thanks 谢谢

First, make your arrays into arrays of Int instead of String . 首先,将数组变成Int而不是String

let redCell = [0, 1, 4]
let greenCell = [2, 3]

Now update your cellForRowAt to check if indexPath.row is in a given array: 现在更新您的cellForRowAt以检查indexPath.row是否在给定数组中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "playQuizTableViewCell") as! playQuizTableViewCell

    if redCell.contains(indexPath.row) {
        cell.textLabel?.backgroundColor = .red
    } else if greenCell.contains(indexPath.row) {
        cell.textLabel?.backgroundColor = .green
    } else {
        cell?.textLabel?.backgroundColor = .black
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS swift4中更改特定的UITableviewcell颜色 - How to change particular UITableviewcell colour in iOS swift4 如何使用SpriteKit以编程方式快速更改背景颜色 - How do I change background colour programmatically in swift using SpriteKit 如何根据Swift中的数组内容对字典数组进行排序 - How do I sort an array of dictionary according to an array contents in Swift 如何将索引处的对象移动到数组的最后一个,同时快速更改相同单元格的颜色? - How to shift object at index to last of an array , also change the colour of cell of the same in swift? 如何更改 tableview 中部分标题的颜色? - How do you change the colour of a section title in a tableview? 如何根据其中的UITextView的大小调整UITableViewCell的大小? - How do I resize a UITableViewCell according to the size of a UITextView inside it? 如何根据UITextView子视图的大小调整UITableViewCell的大小? - How do I resize a UITableViewCell according to the size of a UITextView subView? 如何在UITableViewCell中模糊(ios7样式)图像的一部分? - How do I blur (ios7 style) a section of an image in a UITableViewCell? 如何在 Swift 中以编程方式更改自定义 UITableViewCell 中按钮的背景颜色? - How do I change the background color of a button within a custom UITableViewCell programatically in Swift? 如何更改UITableViewCell的高度? - How do I change UITableViewCell height?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM