3

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

}
saner
  • 821
  • 2
  • 10
  • 32

1 Answers1

8

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()
whyceewhite
  • 6,317
  • 7
  • 43
  • 51
  • 2
    Still the same, I guess the count of array does not match with the numberofrowsinsection part. – saner Sep 19 '15 at 16:53
  • I put some println( )s to understand where the problem starts. The error starts just after the "query.limit = 10" part. The last println is shown there. – saner Sep 19 '15 at 17:04
  • 2
    I just noticed that you cleared your arrays before getting your data. Since the fetch is running asynchronously, you should clear your arrays once you have your data. Does that make since? See the updated code. – whyceewhite Sep 19 '15 at 17:10