菜鸟教程小白 发表于 2022-12-11 16:41:29

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


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

<pre><code>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(&#34;authToken&#34;) as! String

    values[&#34;auth_code&#34;] = &#34;\(authToken)&#34;

    self.checkSync()

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

}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; 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: &#34;syncing&#34;)) {

      let idString: String = &#34;\(cell.idParam!)&#34;
      tokenAndIds.addObject(idString)
    }



    if (syncedIds != nil) {
      for i in 0 ..&lt; syncedIds.count {
            if (syncedIds as! String == &#34;\(cell.idParam!)&#34;) {

                print(&#34;Synced Ids are: \(syncedIds)&#34;)

                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(, withRowAnimation: .Automatic)
                                  cell.syncImageView.image = UIImage(named: &#34;sync&#34;)

            }
      }
    }

    return cell
}

func checkSync() {

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


      let myUrl = NSURL(string: &#34;http://10.0.0.4:81/iphone/sync.php&#34;)

      let request = NSMutableURLRequest(URL: myUrl!)
      request.HTTPMethod = &#34;POST&#34;

      request.addValue(&#34;application/json&#34;, forHTTPHeaderField: &#34;Content-Type&#34;)
      request.addValue(&#34;application/json&#34;, forHTTPHeaderField: &#34;Accept&#34;)

      values[&#34;idArray&#34;] = 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(&#34;error=\(error!)&#34;)
                        return
                  }
                  if let parseJSON = json {


                        for (key, value) in parseJSON {

                            if (value as! String == &#34;0&#34;) {
                              print(key)

                              self.syncedIds.addObject(key)
                            }
                        }

                  }
                } catch {
                  print(&#34;Error is Here: \(error)&#34;)
                }
            }
      }
      task.resume()
    }

}
</code></pre>

<p>但问题是,当我收到有关所有同步 ID 的响应,然后我进入另一个 View ,然后返回对 syncImageView 进行更改时,单元格继续消失,我也收到此警告:“不被重复使用的单元格的索引路径。”我知道这与 cellForRowAtIndexPath 方法有关,但它是什么?另外,如果我停止应用程序并重新运行它,那么一切都会像 imageView 一样被修复并且没有单元格消失。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题是 <code>cellForRowAtIndexPath</code> 中的异步调用。您需要获取您的代码(在 tableView 数据源和委托(delegate)之外),一旦您获取了数据(并且可能存储在某种类型的集合中,如数组),请在您的 tableView 上调用 reloadData() 并刷新数据。</p>

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

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

<p><strong>提示:</strong></p>

<p>请记住,在后台线程中获取数据后,请务必在主线程上调用 reloadData()</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 核心数据 : no index path for table cell being reused, 单元格消失,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37697871/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37697871/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 核心数据 : no index path for table cell being reused, 单元格消失