我更改了tableview单元格选择颜色,它仅在第1行及以后有效,但第0行“first”不显示默认的浅灰色。
我做错了吗?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// Configure the cell...
let colorView = UIView()
let green = UIColor(red:0.31, green:0.62, blue:0.53, alpha:1.0)
colorView.backgroundColor = green
UITableViewCell.appearance().selectedBackgroundView = colorView
cell.textLabel?.text = "#" + books[indexPath.row]
return cell
}
最佳答案
您是否知道通过此调用UITableViewCell.appearance().selectedBackgroundView = colorView
更改了所有单元格的外观?因此,每次您的表视图要求一个单元格时,您都会创建一个新视图,将其称为colorView
并为所有先前创建的单元格替换selectedBackgroundView
吗?你正在做它。
移动这个
let colorView = UIView()
let green = UIColor(red:0.31, green:0.62, blue:0.53, alpha:1.0)
colorView.backgroundColor = green
UITableViewCell.appearance().selectedBackgroundView = colorView
到您的
viewDidLoad
方法。但是,仅当您不仅需要选定单元格的绿色,而且需要更复杂的颜色时,它才可以。
最好在
cellForRowAtIndexPath
中这样做cell.selectedColor = UIColor(red:0.31, green:0.62, blue:0.53, alpha:1.0)
关于ios - table view单元格选择颜色在0个单元格之后有效,但在第一行不起作用吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32971559/