简体   繁体   中英

No index path for table cell being reused in Swift

I noticed there are many questions on this in Obj-C but I hardly remember Obj-C and each of the answers was specific to the question. Here I get this error: "No index path for table cell being reused" sometimes when the app refreshes. I notice that when I don't refresh but I leave and reopen the table view the formatting is ruined.

Here is my "refresh" method used in a few places:

   @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()
            }
        }
    }

And the tableview method:

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
}

Any idea what is causing the error?

What's causing the problem for you: when you scroll your table, the TableView dequeues the cell that your async call to parse server is trying to manipulate.

You can overcome this problem by: 1- in your Sweets table on Parse, store PFUser object as a pointer 2- in your loadData function, fetch user from the query by includeKey method of PFQuery

If you change according to this, you won't have to query the PFUser every time the cellForRowAtIndexPath is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM