2016-07-05 56 views
-1

如果我嘗試對單元格執行操作,則每重複第8行。無法防止重用UITableViewCell中的單元格:Swift

extension ExploreDetailViewController: UITableViewDataSource { 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return specificItemInfo.count 

} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return specificItemInfo[section].subItemArray!.count 

} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("PriceCell", forIndexPath: indexPath) as! PriceTableViewCell 
     cell.configureCell1(specificItemInfo[indexPath.section].subItemArray![indexPath.row], specificItemInfo: specificItemInfo[(indexPath.section)]) 
     cell.delegate = self 
     return cell 
    } 

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell") as! ItemTableViewCell 
    cell.configureCell(specificItemInfo[section]) 
    return cell 
} 

在單元格內部,我使用一個按鈕來設置標籤的值。但是爲一個單元格設置的值又是8個單元格。有什麼辦法可以防止這種情況發生?

+0

每次加載單元格時都要重新設置標籤。所以在你調用cell.configureCell1之前。將標籤重置爲cell.yourlabel.text =「」 – Devster101

+0

您也可以重寫單元格子類上的'-prepareForReuse:'以實現相同的結果 –

+0

@RichTolley - prepareForReuse不應該用於此目的。這是來自官方UITableView文檔的引用。 「出於性能原因,您應該只重置與內容無關的單元格屬性,例如alpha,編輯和選擇狀態。」這是一個鏈接 - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse – itskoBits

回答

1

在細胞內,我用一個按鈕來設置標籤

模型 - 視圖 - 控制器的價值!正如你所說,問題在於單元格被重用在另一行中。解決方案是在使用按鈕時將新標籤值存儲在模型中,以便下次調用configureCell1時,它會將此行的正確值從模型中拉出。 千萬不要使用單元本身存儲任何東西!這只是一個視圖。只能在型號中存儲。

相關問題