[英]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.