gpt4 book ai didi

ios - 更改 UITableView 单元格颜色

转载 作者:行者123 更新时间:2023-11-29 01:49:57 24 4
gpt4 key购买 nike

我有以下代码:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if (self.from! == "Hello") {
println("Hello")
cell.backgroundColor = UIColor.redColor()
cell.textLabel?.textColor = UIColor.yellowColor()
} else {
println("Not Hello")
cell.backgroundColor = UIColor.yellowColor()
cell.textLabel?.textColor = UIColor.redColor()
}
}

问题是它适用于整个表格 - 我只想更改最后创建的单元格。

有什么想法吗?

最佳答案

您需要确定表格中有多少行并将颜色更改仅应用到该行。我看到您正在尝试更改 willdisplaycell 中的颜色。如果您愿意,可以在创建单元格时执行此操作。我假设您使用的是可重复使用的细胞。您知道您的部分中有多少行,因为这是在

中设置的
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
return myData.count
}

然后在创建单元格时:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! UITableViewCell
cell.textLabel?.text = myData[indexPath.row]
if (indexPath.row == myData.count-1) {
cell.backgroundColor = UIColor.yellowColor()
return cell
}

关于ios - 更改 UITableView 单元格颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com