-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個單元格。有什麼辦法可以防止這種情況發生?
每次加載單元格時都要重新設置標籤。所以在你調用cell.configureCell1之前。將標籤重置爲cell.yourlabel.text =「」 – Devster101
您也可以重寫單元格子類上的'-prepareForReuse:'以實現相同的結果 –
@RichTolley - prepareForReuse不應該用於此目的。這是來自官方UITableView文檔的引用。 「出於性能原因,您應該只重置與內容無關的單元格屬性,例如alpha,編輯和選擇狀態。」這是一個鏈接 - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse – itskoBits