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

时间:2015-07-13 15:55:31

标签: ios swift uitableview

我有表视图控制器,我为所有单元格分配颜色,但我想分别为每一行分配颜色。我的细胞原型不是静止的如果任何人可以帮助我,我会很高兴。谢谢。 (我用过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 个答案:

答案 0 :(得分:5)

你走在正确的轨道上。您有几种方法可以继续。

  1. 您可以创建或获取UIColor中使用的cellForRowAtIndexPath(_:)数组,以根据其位置设置每个单元格的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
    }
    
  3. 上面的代码片段会创建具有交替的蓝色和透明蓝色单元格的单元格。

    请注意,第一个选项可能会有更好的效果,因为它会将工作从经常调用的方法(cellForRowAtIndexPath(_:))卸载到一个非常不频繁调用的方法({{1} },例如)。

    修改

    在对原始问题的评论中,您声明要定义5种颜色并将其分配给某些单元格。上面的选项1对此最有效。

    viewDidLoad()类中,声明一个包含ThirdTableViewController个对象的数组。

    UIColor

    然后,在let colors = [UIColor.redColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.orangeColor(), UIColor.purpleColor()] 中,使用它根据cellForRowAtIndexPath(_:)从该数组中选择一种颜色。例如,

    indexPath