【问题标题】:No index path for table cell being reused in Swift没有在 Swift 中重用的表格单元格的索引路径
【发布时间】:2014-10-17 12:06:37
【问题描述】:

我注意到在 Obj-C 中有很多关于此的问题,但我几乎不记得 Obj-C 并且每个答案都是针对该问题的。在这里,我有时会在应用程序刷新时收到此错误:“没有重复使用表格单元格的索引路径”。我注意到当我不刷新但我离开并重新打开表格视图时,格式被破坏了。

这是我在几个地方使用的“刷新”方法:

   @IBAction func loadData(){
        timeLineData.removeAllObjects()
        //pulls the data from the server
        var findTimeLineData: PFQuery = PFQuery(className: "Sweets")
        findTimeLineData.findObjectsInBackgroundWithBlock{
            (objects:[AnyObject]!, error: NSError!) -> Void in
            if !error{
                for object:PFObject! in objects{
                    self.timeLineData.addObject(object)
                }
                let tempArray: NSArray = self.timeLineData.reverseObjectEnumerator().allObjects
                self.timeLineData = tempArray as NSMutableArray

                //reloads the data in the table view
                self.tableView.reloadData()
            }
        }
    }

还有tableview方法:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
    let cell: SweetTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SweetTableViewCell

    let sweet: PFObject = self.timeLineData.objectAtIndex(indexPath.row) as PFObject

    //part of the animation
    cell.sweetTextView.alpha = 0
    cell.userNameLabel.alpha = 0
    cell.timestampLabel.alpha = 0


    cell.sweetTextView.text = sweet.objectForKey("content") as String

    //add the date
    var dateFormatter: NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "HH:mm yyyy-MM-dd"
    cell.timestampLabel.text = dateFormatter.stringFromDate(sweet.createdAt)

    //finds the sweeter associated with a pointer
    var findSweeter: PFQuery = PFUser.query()
    findSweeter.whereKey("objectId", equalTo: sweet.objectForKey("sweeter").objectId)

    findSweeter.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]!, error: NSError!)-> Void in
        if !error{
            let user: PFUser = (objects as NSArray).lastObject as PFUser
            cell.userNameLabel.text = user.username
        }

    }

    //adds animation
    UIView.animateWithDuration(1, animations: {
        cell.sweetTextView.alpha = 1
        cell.userNameLabel.alpha = 1
        cell.timestampLabel.alpha = 1
    })

    return cell
}

知道是什么导致了错误吗?

【问题讨论】:

  • 我遇到了同样的问题,在我的情况下,当我删除应用程序中的所有内容时就会发生这种情况。

标签: ios uitableview swift tableview


【解决方案1】:

是什么导致了您的问题:当您滚动表格时,TableView 将您的异步调用解析服务器试图操作的单元格出列。

您可以通过以下方式解决此问题: 1-在 Parse 上的 Sweets 表中,将 PFUser 对象存储为指针 2- 在您的 loadData 函数中,通过 PFQuery 的 includeKey 方法从查询中获取用户

如果按照这个改变,就不用每次调用cellForRowAtIndexPath都去查询PFUser了。

【讨论】:

    猜你喜欢
    • 2013-08-31
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多