ios - UITableView 单元格重复

标签 ios swift uitableview

我正在开发我的第一个 Swift 应用程序,并且永远陷入了一个错误。向核心数据添加新条目时,第一次一切正常。但是,对于其他项目,先前添加的项目会在表中重复。

数据不重复,只有单元格。重新加载应用程序时,单元格会正确显示。

这是填充单元格的代码:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if let fetchedSections: AnyObject = fetchedResultController.sections as AnyObject? {
        return fetchedSections.count
    } else {
        return 0
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let fetchedSections: AnyObject = fetchedResultController.sections as AnyObject? {
        return fetchedSections[section].numberOfObjects
    } else {
        return 0
    }
}

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

    let lookup = sortedArray[indexPath.row] as! NSManagedObjectID
    let spot = spotsDict[lookup] as! Spots
    let cell = tableView.dequeueReusableCellWithIdentifier("SpotCell", forIndexPath:indexPath) as! SpotCell

    println(sortedArray)
    println(spot)

    cell.spotLabel.text = spot.title

    cell.spotPhoto.image = self.imagesDict[lookup]

    cell.distanceLabel.text = self.distanceStringDict[lookup] as NSString! as String

    cell.spotPhoto.layer.cornerRadius = 4
    cell.spotPhoto.clipsToBounds = true

    return cell
}

最佳答案

请替换您的代码

让 lookup = sortedArray[indexPath.row] 为! NSManagedObjectID

在 cellForRowAtIndexPath 方法中使用以下代码。

让 lookup = sortedArray[indexPath.section] 为! NSManagedObjectID

关于ios - UITableView 单元格重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30802298/

相关文章:

ios - iOS Swift 项目中的 UIWebView 显示白屏并输出 "error in __connection_block_invoke_2: Connection interrupted"

ios - 启用位代码时生成错误; ld : -bundle and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together

ios - 构建由其他 UIView 组成的 UIButton 的正确方法是什么?

ios - iOS 和 MacOS 之间通信的框架

ios - 在模态视图 Controller 上呈现整个屏幕 View Controller

ios - 在运行时更改 UITableView 行的部分

iphone - UITextView 自动高度

ios - 在 iOS 11 ARKit 中获取相机视野

ios - 该类与键 tableView 的键值编码不兼容

ios - TableView 的 cellForRowAtIndexPath : not being called