-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