saner
saner

Reputation: 821

"Index Out Of Range" While refreshing table - Swift

I have two buttons in the view controller and a table view below them. The table view is refreshed when one of the two buttons is pressed with different filter parameters. Actually when the table view is loaded and then the other button is clicked it works fine and table is refreshed. However when you scroll in the table view and while the scrolling is still happening you push on the other button, I get the "index out of range" error. Tried lots of things to solve it but just couldn't. Any ideas? I use parse as the free membership. The important parts of my code is as below:

  @IBAction func filterType0_clicked(sender: AnyObject) {

    if filterTypeVar != 0 {


        self.activityIndicator.startAnimating()
        self.waitView.hidden = false

    filterTypeVar = 0
    refreshResults()
    }
}

@IBAction func filterType1_clicked(sender: AnyObject) {

    if filterTypeVar != 1 {

        self.activityIndicator.startAnimating()
        self.waitView.hidden = false

    filterTypeVar = 1
    refreshResults()
    }
}



func refreshResults() {


    resultsNameArray.removeAll(keepCapacity: false)
    resultsUserNameArray.removeAll(keepCapacity: false)
    resultsObjectIdArray.removeAll(keepCapacity: false)


    var query = PFQuery(className: “posts”)
    query.whereKey("userName", containedIn: usersArray)

    if filterTypeVar == 0 {
    println("no filter")
    }
    else {
    query.whereKey("Type", equalTo: filterTypeVar)
    }

    query.addDescendingOrder("createdAt")
    query.limit = 10

    query.findObjectsInBackgroundWithBlock {
        (objects:[AnyObject]?, error:NSError?) -> Void in

        if error == nil {

            for object in objects! {

                self.resultsNameArray.append(object.objectForKey("profileName") as! String)
                self.resultsUserNameArray.append(object.objectForKey("userName") as! String)
                self.resultsObjectIdArray.append(object.objectId as String!)

                      self.resultsTable.reloadData()

            }

            if self.resultsNameArray.isEmpty {
            self.resultsTable.hidden = true
            }

            else {
            self.resultsTable.hidden = false
            }



            self.activityIndicator.stopAnimating()
            self.waitView.hidden = true


        }

    }

}



func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    return resultsNameArray.count

}



func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

            return 127

}






func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:mainCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! mainCell



    cell.profileLbl.setTitle(self.resultsNameArray[indexPath.row], forState: UIControlState.Normal)
    cell.usernameLbl.text = self.resultsUserNameArray[indexPath.row]
    cell.objectid.text = self.resultsObjectIdArray[indexPath.row]


    return cell

}

Upvotes: 3

Views: 3853

Answers (1)

whyceewhite
whyceewhite

Reputation: 6425

Try reloading your table data after you have finished fetching and filling out the data arrays. Also, clear your data arrays once you have your query data.

For example, in your refreshResults method, move the self.resultsTable.reloadData() outside of the for loop:

resultsNameArray.removeAll(keepCapacity: false)
resultsUserNameArray.removeAll(keepCapacity: false)
resultsObjectIdArray.removeAll(keepCapacity: false)
// You could reload again here; this might be superfluous.
self.resultsTable.reloadData()

for object in objects! {

    self.resultsNameArray.append(object.objectForKey("profileName") as! String)
    self.resultsUserNameArray.append(object.objectForKey("userName") as! String)
    self.resultsObjectIdArray.append(object.objectId as String!)
}

// Moved the refresh outside of loop so that you
// aren't reloading the table until you've finished
// loading your data.
self.resultsTable.reloadData()

Upvotes: 8

Related Questions