为动态表格中的一个单元格设置背景
问题描述:
我有一个TableView
动态原型,并且希望第一个单元格具有与其他单元格不同的背景。为动态表格中的一个单元格设置背景
我尝试是添加
if indexPath.row == 0 {
cell.backgroundView = UIImageView(image: UIImage(named: "firstCellBackground"))
}
在
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
这实际上是一种工作。第一个单元格获得不同的背景,但只要我开始滚动,其他几个单元格也会获得背景。 :(
任何建议
答
UITableViewCell
情况下,通常重复使用性能方面的原因;。最初显示的行0该小区以后可能例如显示行11如果您使用的UITableViewCell子类,你可以在其中实现prepareForReuse()
backgroundView
可以被清除或者你可以试试这个:
if indexPath.row == 0 {
cell.backgroundView = UIImageView(image: UIImage(named: "firstCellBackground"))
} else {
cell.backgroundView = nil
}
+1
这两个建议都在起作用。 – eLwoodianer
不能使用半逻辑;你必须完全说明什么后台视图是_every_行,因为相同的细胞将被用于许多不同的行(其就是为什么你看到其他细胞获得背景) – matt