2015-11-17 156 views
0

我有这个问题,其中原型动态UITableViewCell上的数据值在向上滚动或向下滚动时被重置。UITableViewCell - 向上滚动或向下滚动时重置内容

我检查的单元格是零,因为建议在其他线程,因为单元格被重用,但执行从未击中,如果。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // Currency formatter 
    let formatter = NSNumberFormatter() 
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle 
    formatter.locale = NSLocale(localeIdentifier: "el-GR") 

    // Table view cells are reused and should be dequeued using a cell identifier. 
    let cellIdentifier = "MenuItemTableViewCell" 

    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MenuItemTableViewCell 

    if cell.isEqual(nil) { 
     cell = UITableViewCell(style: .Default, reuseIdentifier: "MenuItemTableViewCell") as! MenuItemTableViewCell 
    } 

    // Match category (section) with items from data source 
    let itemsPerSection = items.filter({ $0.category == self.categories[indexPath.section] }) 
    let item = itemsPerSection[indexPath.row] 

    // cell data 
    cell.cellTitle.text = item.name + " " + formatter.stringFromNumber(item.price)! 
    cell.cellDescription.text = item.description 

请帮忙吗?

问候, 城邦

+0

单元格内容在屏幕上时是否重置?究竟发生了什么?这种方法看起来很好,还有什么其他方法与表视图交互? –

+0

是的,重置发生在我向下滚动然后再次备份时。例如,我有一个自定义的步进控件,带有一个表示菜单项(食物)数量的标签。我将它设置为2,向下滚动(以便单元格不在视图中),再次向上滚动并且它是0.我在我的tableview中有多个部分,所以我也实现了titleForHeaderInSection和numberOfRowsInSection。没什么特别的。 – Polis

+0

我之前有过这个问题。我现在使用翠鸟开源框架,它工作得很好。此外,它支持异步图像,这将使您的应用程序快很多,没有任何代码从你身边 –

回答

0

细胞每次熄灭屏幕,并重新打开电池被重新创建。因此,如果您修改单元格(即按照您使用步进器所描述的那样),则必须保存该修改后的值,以便在下一次出现在屏幕上时使用该修改后的值创建该值。

+0

你能举个例子吗?我何时/将在哪里保存修改后的值(即哪种委托方法),以及如何知道下一次细胞进入屏幕时是新的还是同一个?这是我真正困惑的地方。 – Polis

+0

每当单元格离开屏幕时,当它返回到屏幕上时,您将调用“tableView:cellForItemAtIndexPath:'。这意味着用于在'tableView:cellForItemAtIndexPath:'中设置单元格值的变量必须始终保持最新。您将更新它们以响应用户操作该值(即,在您描述的情况下,步进器的'IBAction')。 –