gpt4 book ai didi

ios - 如何在swift中为每一行分配不同的颜色

转载 作者:搜寻专家 更新时间:2023-11-01 06:08:22 25 4
gpt4 key购买 nike

我有 TableView Controller ,我为所有单元格分配了颜色,但我想分别为每一行分配颜色。我的细胞原型(prototype)不是静态的如果有人能帮助我,我将不胜感激。谢谢你。 (我用的是swift)

import UIKit

class ThirdTableViewController: UITableViewController {

var dataPassed:String!
var kitap = [String]()

override func viewDidLoad() {
super.viewDidLoad()


switch dataPassed {

case "1":

kitap = [
"Yaratılış Kitabı","Mısır'dan çıkış","Levililer",
"Çölde sayım",
"Yasanın tekrarı",
"Yeşu",
"Hakimler",
"Rut",
"I.Samuel",
"II.Samuel",
"I.Krallar",
"II.Krallar",
"I.Tarihler Adem'in soyu",
"II.Tarihler",
"Ezra",
"Nehemya",
"Ester",
"Eyüp",
"Özdeyişler",
"Vaiz",
"Ezgiler ezgisi",
"Yeşaya",
"Yeremya",
"Ağıtlar",
"Hezekiel",
"Daniel",
"Hoşea",
"Yoel",
"Amos",
"Ovadya",
"Yunus Rab'den kaçıyor",
"Mika",
"Nahum",
"Habakkuk",
"Sefanya",
"Hagay",
"Zekeriya",
"Malaki"
]



case "2":

kitap = ["001 Mezmur","002 Mezmur","003 Mezmur","004 Mezmur","005 Mezmur","006 Mezmur","007 Mezmur","008 Mezmur","009 Mezmur","010 Mezmur","011 Mezmur","012 Mezmur","013 Mezmur","014 Mezmur","015 Mezmur","016 Mezmur","017 Mezmur","018 Mezmur","019 Mezmur","020 Mezmur","021 Mezmur","022 Mezmur","023 Mezmur","024 Mezmur","025 Mezmur","026 Mezmur","027 Mezmur","028 Mezmur","029 Mezmur","030 Mezmur","031 Mezmur","032 Mezmur","033 Mezmur","034 Mezmur","035 Mezmur","036 Mezmur","037 Mezmur","038 Mezmur","039 Mezmur","040 Mezmur","041 Mezmur","042 Mezmur","043 Mezmur","044 Mezmur","045 Mezmur","046 Mezmur","047 Mezmur","048 Mezmur","049 Mezmur","050 Mezmur","051 Mezmur","052 Mezmur","053 Mezmur","054 Mezmur","055 Mezmur","056 Mezmur","057 Mezmur","058 Mezmur","059 Mezmur","060 Mezmur","061 Mezmur","062 Mezmur","063 Mezmur","064 Mezmur","065 Mezmur","066 Mezmur","067 Mezmur","068 Mezmur","069 Mezmur","070 Mezmur","071 Mezmur","072 Mezmur","073 Mezmur","074 Mezmur","075 Mezmur","076 Mezmur","077 Mezmur","078 Mezmur","079 Mezmur","080 Mezmur","081 Mezmur","082 Mezmur","083 Mezmur","084 Mezmur","085 Mezmur","086 Mezmur","087 Mezmur","088 Mezmur","089 Mezmur","090 Mezmur","091 Mezmur","092 Mezmur","093 Mezmur","094 Mezmur","095 Mezmur","096 Mezmur","097 Mezmur","098 Mezmur","099 Mezmur","100 Mezmur","101 Mezmur","102 Mezmur","103 Mezmur","104 Mezmur","105 Mezmur","106 Mezmur","107 Mezmur","108 Mezmur","109 Mezmur","110 Mezmur","111 Mezmur","112 Mezmur","113 Mezmur","114 Mezmur","115 Mezmur","116 Mezmur","117 Mezmur","118 Mezmur","119 Mezmur","120 Mezmur","121 Mezmur","122 Mezmur","123 Mezmur","124 Mezmur","125 Mezmur","126 Mezmur","127 Mezmur","128 Mezmur","129 Mezmur","130 Mezmur","131 Mezmur","132 Mezmur","133 Mezmur","134 Mezmur","135 Mezmur","136 Mezmur","137 Mezmur","138 Mezmur","139 Mezmur","140 Mezmur","141 Mezmur","142 Mezmur","143 Mezmur","144 Mezmur","145 Mezmur","146 Mezmur","147 Mezmur","148 Mezmur","149 Mezmur","150 Mezmur"]


default:

kitap = ["Veri Yok"]


}


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return kitap.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("carCell", forIndexPath: indexPath) as! UITableViewCell

cell.textLabel!.text = kitap[indexPath.row]

switch dataPassed {

case "1":
self.view.backgroundColor = UIColor(red: 0.3529, green: 0.7608, blue: 0.8471, alpha: 1.0)
cell.backgroundColor = UIColor(red: 0.3529, green: 0.7608, blue: 0.8471, alpha: 1.0)
cell.textLabel?.textColor = UIColor.whiteColor()
case "2":
self.view.backgroundColor = UIColor(red: 0.9882, green: 0.5804, blue: 0.0078, alpha: 1.0)
cell.backgroundColor = UIColor(red: 0.9882, green: 0.5804, blue: 0.0078, alpha: 1.0)
cell.textLabel?.textColor = UIColor.whiteColor()

default:

cell.backgroundColor = UIColor.whiteColor()



}

return cell

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if(segue.identifier != "backbutton") {

let cell = sender as! UITableViewCell
let indexPath = self.tableView.indexPathForCell(cell)

let name = kitap[indexPath!.row]

let destination = segue.destinationViewController as! ThirdViewController

destination.firstvalue = name

}



}


}

最佳答案

您走在正确的轨道上。对于如何继续,您有几个选择。

  1. 您可以创建或获取您在 cellForRowAtIndexPath(_:) 中使用的 UIColor 数组,以设置 backgroundColor 属性每个单元格的位置取决于它的位置。在下面的代码片段中,UIColor 数组是一个名为colors 的实例变量。所以 self.colors[indexPath.row] 根据单元格的索引路径检索颜色。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("carCell", forIndexPath: indexPath)

    cell.textLabel!.text = kitap[indexPath.row]

    switch dataPassed {

    case "1":
    self.view.backgroundColor = UIColor(red: 0.3529, green: 0.7608, blue: 0.8471, alpha: 1.0)
    cell.backgroundColor = self.colors[indexPath.row]
    cell.textLabel?.textColor = UIColor.whiteColor()
    case "2":
    self.view.backgroundColor = UIColor(red: 0.9882, green: 0.5804, blue: 0.0078, alpha: 1.0)
    cell.backgroundColor = self.colors[indexPath.row]
    cell.textLabel?.textColor = UIColor.whiteColor()

    default:
    cell.backgroundColor = UIColor.whiteColor()
    }

    return cell
    }
  2. 您的另一个选择是在 cellForRowAtIndexPath(_:) 中“即时”计算颜色。这可以通过随机数生成器来完成,但我假设您想要一个更有条理的配色方案。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("carCell", forIndexPath: indexPath)

    cell.textLabel!.text = kitap[indexPath.row]

    let color = UIColor.blueColor().colorWithAlphaComponent(indexPath.row % 2 == 0 ? 1.0 : 0.7)

    switch dataPassed {

    case "1":
    self.view.backgroundColor = UIColor(red: 0.3529, green: 0.7608, blue: 0.8471, alpha: 1.0)
    cell.textLabel?.textColor = UIColor.whiteColor()
    case "2":
    self.view.backgroundColor = UIColor(red: 0.9882, green: 0.5804, blue: 0.0078, alpha: 1.0)
    cell.textLabel?.textColor = UIColor.whiteColor()

    default: break
    }

    cell.backgroundColor = color

    return cell
    }

上面的代码片段将创建具有交替的蓝色和透明蓝色单元格的单元格。

请注意,第一个选项可能会执行得更好,因为它将工作从经常调用的方法 (cellForRowAtIndexPath(_:)) 转移到很少调用的方法 (viewDidLoad( ),例如)。

编辑

在对您的原始问题的评论中,您说您想要定义 5 种颜色并将它们分配给某些单元格。上面的选项 1 最适合这个。

在您的 ThirdTableViewController 类中,声明一个包含 UIColor 对象的数组。

let colors = [UIColor.redColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.orangeColor(), UIColor.purpleColor()]

然后,在 cellForRowAtIndexPath(_:) 中,使用它根据 indexPath 从该数组中选择一种颜色。例如,

cell.backgroundColor = self.colors[indexPath.row % self.colors.count]

// ...

return cell

关于ios - 如何在swift中为每一行分配不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com