2016-05-14 92 views
0

我有一个TableView动态原型,并且希望第一个单元格具有与其他单元格不同的背景。为动态表格中的一个单元格设置背景

我尝试是添加

if indexPath.row == 0 { 
    cell.backgroundView = UIImageView(image: UIImage(named: "firstCellBackground")) 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

这实际上是一种工作。第一个单元格获得不同的背景,但只要我开始滚动,其他几个单元格也会获得背景。 :(

任何建议

+1

不能使用半逻辑;你必须完全说明什么后台视图是_every_行,因为相同的细胞将被用于许多不同的行(其就是为什么你看到其他细胞获得背景) – matt

回答

2

UITableViewCell情况下,通常重复使用性能方面的原因;。最初显示的行0该小区以后可能例如显示行11如果您使用的UITableViewCell子类,你可以在其中实现prepareForReuse()backgroundView可以被清除或者你可以试试这个:

if indexPath.row == 0 { 
    cell.backgroundView = UIImageView(image: UIImage(named: "firstCellBackground")) 
} else { 
    cell.backgroundView = nil 
} 
+1

这两个建议都在起作用。 – eLwoodianer

相关问题