2016-04-19 19 views
0

私はXcode 6(Swift 1)でアプリを作っています。私は特定の細胞のテキストの色を緑色にしようとしていますが、他の色は赤くしていますが、うまくいきません。ここに私のコードは次のとおりです。UITableViewの特定のセルのテキストの色を変更するにはどうすればよいですか?

import Foundation 
import UIKit 

class ViewController: UIViewController, UITableViewDelegate,  UITableViewDataSource { 
@IBOutlet var tableView: UITableView! 
var items: [String] = ["green", "red"] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.items.count; 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

    cell.textLabel?.text = self.items[indexPath.row] 

    return cell 
} 

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
    println("You selected cell #\(indexPath.row)!") 
} 
} 

そして、ここでは、ユニバーサルテキストの色を変更するための私のコードは次のとおりです。

cell.textLabel.textColor = [UIColor greenColor]; 

ので、例えば、どのように私は緑の「緑」のセル、および「赤」を作ることができますセル赤?ありがとう!

+0

を使用すると、セルのtextLabelの色を変更している場合は? – HeavenlyManBR

+0

"var items"宣言後 – 9fiftyfive

答えて

2

var items宣言した後、これを追加します。tableView(_:cellForRowAtIndexPath:)

let itemColors = [UIColor.greenColor(), UIColor.redColor()] 

は、returnステートメントの前にこれを追加します。

cell.textLabel?.textColor = itemColors[indexPath.row] 
+0

ありがとう!しかし、2つのこと。まず、2行目のコード "UILabel?"を使用してエラーが発生します。 "textColor"という名前のメンバーはありません。第二に、私は、括弧の間に緑色のアイテムを置くと仮定しています。 – 9fiftyfive

+0

私は自分の答えを更新しました。項目の色を 'itemColors'配列に置きます。 –

+0

「itemColors = [UIColor.greenColor()、UIColor.redColor()]」括弧内にアイテムを配置するときにもエラーが発生します。私は何かが間違っていると思う – 9fiftyfive

関連する問題