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
}