No index path for table cell being reused in Swift
Asked Answered
G

1

6

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?

Goodale answered 23/8, 2014 at 23:28 Comment(1)
I'm having the same issue, in my case it happens when I delete every content in my app.Tampa
C
0

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.

Civil answered 30/4, 2016 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.