我正在开发我的第一个 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/