• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 核心数据 : no index path for table cell being reused, 单元格消失

[复制链接]
菜鸟教程小白 发表于 2022-12-11 16:41:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试制作一个应用程序,它可以在 Core Data 中保存一些条目,并且还可以选择在线同步它。我想要的是,当文档同步时,它在单元格的 imageView 和背景上有不同的图像,我发送请求以获取所有同步文档的唯一 ID。因此,每当文档同步时,它的 imageView 的图像就会发生变化。所以,到目前为止,我已经做到了。

override func viewDidLoad() {
    super.viewDidLoad()
    self.syncedIds = NSMutableArray()
}
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tokenAndIds = NSMutableArray()


    let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
    let authToken = prefs.valueForKey("authToken") as! String

    values["auth_code"] = "\(authToken)"

    self.checkSync()

    NSTimer.scheduledTimerWithTimeInterval(6.0, target: self, selector: #selector(ReceiptsListViewController.checkSync), userInfo: nil, repeats: true)

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


    let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier as String, forIndexPath: indexPath) as! ReceiptsViewCell

    // Configure the cell...

    let entry: DiaryEntry = self.fetchedResultsController.objectAtIndexPath(indexPath) as! DiaryEntry

    cell.configureCellForEntry(entry)
    cell.imageButton.tag = indexPath.row

    if (self.tableView.editing) {
        cell.imageButton.hidden = true
    } else {
        cell.imageButton.hidden = false
    }

    if (cell.syncImageView.image == UIImage(named: "syncing")) {

        let idString: String = "\(cell.idParam!)"
        tokenAndIds.addObject(idString)
    }



    if (syncedIds != nil) {
        for i in 0 ..< syncedIds.count {
            if (syncedIds[i] as! String == "\(cell.idParam!)") {

                print("Synced Ids are: \(syncedIds[i])")

                let cell1 = tableView.dequeueReusableCellWithIdentifier(CellIdentifier as String, forIndexPath: indexPath) as! ReceiptsViewCell

                let entry: DiaryEntry = self.fetchedResultsController.objectAtIndexPath(indexPath) as! DiaryEntry
                entry.sync = 2

                let coreDataStack: CoreDataStack = CoreDataStack.defaultStack
                coreDataStack.saveContext()

                cell1.configureCellForEntry(entry)

                self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                                  cell.syncImageView.image = UIImage(named: "sync")

            }
        }
    }

    return cell
}

func checkSync() {

    if (tokenAndIds != nil && tokenAndIds != []) {
        let idArray: NSMutableArray = tokenAndIds


        let myUrl = NSURL(string: "http://10.0.0.4:81/iphone/sync.php")

        let request = NSMutableURLRequest(URL: myUrl!)
        request.HTTPMethod = "OST"

        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")

        values["idArray"] = idArray

        request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(values, options: [])

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            (let data, let response, let error) in

            if let _ = response as? NSHTTPURLResponse {
                do {
                    let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

                    if error != nil {
                        print("error=\(error!)")
                        return
                    }
                    if let parseJSON = json {


                        for (key, value) in parseJSON {

                            if (value as! String == "0") {
                                print(key)

                                self.syncedIds.addObject(key)
                            }
                        }

                    }
                } catch {
                    print("Error is Here: \(error)")
                }
            }
        }
        task.resume()
    }

}

但问题是,当我收到有关所有同步 ID 的响应,然后我进入另一个 View ,然后返回对 syncImageView 进行更改时,单元格继续消失,我也收到此警告:“不被重复使用的单元格的索引路径。”我知道这与 cellForRowAtIndexPath 方法有关,但它是什么?另外,如果我停止应用程序并重新运行它,那么一切都会像 imageView 一样被修复并且没有单元格消失。



Best Answer-推荐答案


问题是 cellForRowAtIndexPath 中的异步调用。您需要获取您的代码(在 tableView 数据源和委托(delegate)之外),一旦您获取了数据(并且可能存储在某种类型的集合中,如数组),请在您的 tableView 上调用 reloadData() 并刷新数据。

cellForRowAtIndexPath 应该寻找现有数据,而不是获取数据。 indexPath.row 用于调用你的集合索引并获取相应的数据。

当您的异步调用完成时,cellForRowAtIndexPath 已经完成,并且对您尝试更新的单元格的引用不再存在(因此出现错误)。

提示:

请记住,在后台线程中获取数据后,请务必在主线程上调用 reloadData()

关于ios - 核心数据 : no index path for table cell being reused, 单元格消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697871/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap