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?