ios - How to assign different color each row in swift -


i have table view controller , assigned color cell want assign color each row separately. cells prototyped not static if 1 me i'll greatful. thank you. ( used 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 resources can 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          }        }   } 

you're on right track. have couple options how proceed.

  1. you can create or obtain array of uicolor use in cellforrowatindexpath(_:) set backgroundcolor property of each cell depending on position. in code snippet below, uicolor array instance variable named colors. self.colors[indexpath.row] retrieves color based on cell's index path.

    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. your other option calculate colors "on fly" in cellforrowatindexpath(_:). done random number generator, i'll assume want more structured color scheme.

    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 } 

the above code snippet create cells alternating blue , transparent blue cells.

note first option perform better because offload work method that's called (cellforrowatindexpath(_:)) method that's called infrequently (viewdidload(), example).

edit

in comment on original question stated want define 5 colors , assign cells. option 1 above work best this.

in thirdtableviewcontroller class, declare array holds uicolor objects.

let colors = [uicolor.redcolor(), uicolor.bluecolor(), uicolor.greencolor(), uicolor.orangecolor(), uicolor.purplecolor()] 

then, in cellforrowatindexpath(_:), use select color array based on indexpath. example,

cell.backgroundcolor = self.colors[indexpath.row % self.colors.count]  // ...  return cell 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -