【发布时间】:2016-04-19 11:57:25
【问题描述】:
所以,我正在 Xcode 6 (Swift 1) 中制作应用程序。我正在尝试使某些单元格的文本颜色为绿色,而其他单元格的文本颜色为红色,但是,我没有成功。这是我的代码:
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];
那么,例如,如何将“绿色”单元格设为绿色,将“红色”单元格设为红色?谢谢!
【问题讨论】:
-
你在哪里改变单元格 textLabel 颜色?
-
在“var items”声明之后
标签: ios xcode swift uitableview