Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView单元格重复项_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableView单元格重复项

Ios UITableView单元格重复项,ios,swift,uitableview,Ios,Swift,Uitableview,我正在开发我的第一个Swift应用程序,并且一直被一个bug困扰着。在coredata中添加新条目时,第一次一切正常。但是,对于其他项,以前添加的项在表中重复 数据不重复,只复制单元格。重新加载应用程序时,单元格将正确显示 下面是填充单元格的代码: func numberOfSectionsInTableView(tableView: UITableView) -> Int { if let fetchedSections: AnyObject = fetchedResultCon

我正在开发我的第一个Swift应用程序,并且一直被一个bug困扰着。在coredata中添加新条目时,第一次一切正常。但是,对于其他项,以前添加的项在表中重复

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

下面是填充单元格的代码:

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
}

请替换您的代码

let lookup=sortedArray[indepath.row]as!NSManagedObjectID

在cellForRowAtIndexPath方法中使用以下代码


let lookup=sortedArray[indepath.section]as!NSManagedObjectID

问题的根源在于我的排序方法。。将代码简化了很多,改为使用排序描述符进行排序。

请包含您关于
sortedArray
spotsDict
的逻辑-它们与您获取的结果有何关系?是否在“获取结果”控制器上使用自己的排序而不是
sortDescriptors
?如果在表视图中使用“获取结果”控制器,则不应具有其他数据源数组。改用
fetchedResultsController.objectAtIndexPath(indexPath)
。还请注意,
cellforrowatinexpath
只计算索引路径行,而忽略该部分。@johnpatrickmorge我正在尝试进行相对距离排序,因此排序键不存储在coredata中。我已经把它拼凑起来了,这肯定超出了我的技能水平,所以请原谅这一团糟。这是视图:好的。我重新编写了代码,以批量更新加载时核心数据中的相对距离,并使用sortDescriptors进行排序。似乎工作得很好,谢谢你给我指出了正确的方向@johnpatrickmorgani我认为这不是问题您添加了两个以上的对象,看看会发生什么?我曾经在tableview中有两个部分时,在启动按钮操作时遇到问题。。我决定从情节提要中删除两个单元格……这只会导致所有单元格显示相同的数据。@RajeshMaurya是的,在第一个对象之后,它总是重复先前添加的一个。因此,在添加第三个之后,第一个和第二个都显示两次。