0

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

我正在检查单元格是否为 nil,正如其他线程中所建议的那样,因为单元格被重用但执行永远不会达到那个如果。

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

请问有什么帮助吗?

问候, 波利斯

4

1 回答 1

1

每次单元格离开屏幕并重新打开时,都会重新创建单元格。因此,如果您修改单元格(即,如您使用步进器所描述的那样),您必须保存修改后的值,以便下次出现在屏幕上时使用修改后的值创建它。

于 2015-11-18T22:36:58.700 回答