ios - UITableViewCell - 向上或向下滚动时内容重置

标签 ios xcode uitableview scroll swift2

我遇到这个问题,当向上或向下滚动时,原型(prototype)动态 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

有什么帮助吗?

问候, 城邦

最佳答案

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

关于ios - UITableViewCell - 向上或向下滚动时内容重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33769102/

相关文章:

ios - ipad 应用程序中的代理调用非常慢

objective-c - 在 Windows 中运行 Objective C(通过 XCode 制作)

xcode - 以编程方式在 Swift 3.0 中创建 NSImageView

ios - Photoshop 的选择性滤色器背后的数学/算法是什么?

ios - AVCaptureVideoPreviewLayer 直接转换为 jpeg 数据?

ios - 从 HealthKit 读取锻炼数据的最佳方法

ios - 为什么子类在框架中不起作用,但扩展在 Swift 中起作用?

iphone - MKMap 不使用定义的纬度和经度

ios - 我怎么能从上到下拖动一个表格 View 然后触发一个 Action 呢?

ios - 无法使 UITableView 出现在半透明导航栏下