2016-07-05 87 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时,它会将此行的正确值从模型中拉出。 千万不要使用单元本身存储任何东西!这只是一个视图。只能在型号中存储。

相关问题