2016-05-14 95 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