2016-08-09 10 views
4

各セルの背景色を順番に変更したいと思います。各uitableviewcellの背景色を変更するには

これは私の色です。そして、私は画像のように表示したいだけです。

私は現在ランダムな色で表示していますが、私は順番に表示したいと思います。

var cellColors = ["F28044","F0A761","FEC362","F0BB4C","E3CB92","FEA375"] 
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! GroupTableViewCell 
     let randomColor = Int(arc4random_uniform(UInt32(self.cellColors.count))) 
     cell.contentView.backgroundColor = UIColor(hexString: self.cellColors[randomColor]) 
    } 

enter image description here

+0

はcellForRowAtIndexPathでそれを行います。いくつかの問題がある場合は、コードを表示してください。 –

+0

私は自分の質問を編集しました –

答えて

6

は、あなたはそれがすでにパラメータであなたの携帯を持っているので、あなたのwillDisplayCell機能からこのライン

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

を削除する必要があり、あなただけの新しいセルでそれを上書きし、新しいセルは決してないであろう中古。

あなたは順番に色を表示したい場合は、あなたがindexPathを使用することができます。

var cellColors = ["F28044","F0A761","FEC362","F0BB4C","E3CB92","FEA375"] 
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.contentView.backgroundColor = UIColor(hexString: cellColors[indexPath.row % cellColors.count]) 
} 
+1

編集:申し訳ありません、私はindexPath.row%cellColors.count thatsが動作していません。 ありがとう –

+0

また、セルの種類はカスタムでなければなりません – ergunkocak

+0

@ ShadowOf私たちはUITableViewRowActionを使ってセルの色を変更できますか?のcell.contentView.backgroundColor = UIColor(hexString:cellColors [indexPath.row%cellColors.count]) }で、color = UITableViewRowAction(スタイル:.normal、title: "Color"){(_、indexPath) } – ArgaPK

0

色を持つ配列を作成し、多分あなたはdynammicallyそれらを作成する必要がありますが、この例では、彼らは、ハードコードされています。

let bgColors = [UIColor.blackColor(), UIColor.grayColor(), UIColor.whiteColor()]; 

次に、cellForRowAtIndexPathに正しいインデックスで色を付け、セルに設定します。あなたの実際のセットアップは分かりませんが、これに似た何かがうまくいくはずです。

let bgColor = bgColors[indexPath.row] 
cell.contentView.backgroundColor = bgColor 
関連する問題