更改 UITableView 中的颜色单元格 以前记录 Swift
Change color cell in UITableView Previously recorded Swift
如何更改已添加的单元格的颜色?
当我更改单元格更改将添加到 UITableView 的颜色时,我需要更改所有 TableView 中单元格的颜色。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
let item = self.items[indexPath.row]
cell.la_view.text = item.list_1
cell.la_view2.text = item.list_2
switch sw {
case 0:
cell.la_view.textColor = UIColor.green
cell.la_view2.textColor = UIColor.green
case 1:
cell.la_view.textColor = UIColor.white
cell.la_view2.textColor = UIColor.white
default:
print("")
}
cell.backgroundColor = UIColor(named: "Defeult")
return cell
}
如果你只有两种颜色,那么你可以只使用布尔变量:
var colorSwitch = false
然后在你的 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
:
if colorSwitch {
cell.la_view.textColor = //color
cell.la_view2.textColor = //color
else {
cell.la_view.textColor = //color
cell.la_view2.textColor = //color
如果要更改的颜色不止一种,可以使用整数值并使用已有的 switch case 语句。
如何更改已添加的单元格的颜色?
当我更改单元格更改将添加到 UITableView 的颜色时,我需要更改所有 TableView 中单元格的颜色。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
let item = self.items[indexPath.row]
cell.la_view.text = item.list_1
cell.la_view2.text = item.list_2
switch sw {
case 0:
cell.la_view.textColor = UIColor.green
cell.la_view2.textColor = UIColor.green
case 1:
cell.la_view.textColor = UIColor.white
cell.la_view2.textColor = UIColor.white
default:
print("")
}
cell.backgroundColor = UIColor(named: "Defeult")
return cell
}
如果你只有两种颜色,那么你可以只使用布尔变量:
var colorSwitch = false
然后在你的 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
:
if colorSwitch {
cell.la_view.textColor = //color
cell.la_view2.textColor = //color
else {
cell.la_view.textColor = //color
cell.la_view2.textColor = //color
如果要更改的颜色不止一种,可以使用整数值并使用已有的 switch case 语句。