视图控制器中有两个按钮,下面有一个表格视图。使用不同的过滤器参数按下两个按钮之一时,将刷新表视图。实际上,当加载表视图然后单击另一个按钮时,它工作正常,表刷新。但是,当您在表格视图中滚动并且滚动仍然发生时,您按下另一个按钮,我得到“索引超出范围”错误。尝试了很多东西来解决它,但事实并非如此。有任何想法吗?我使用解析作为免费会员资格。我的代码的重要部分如下:
@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
}
答案 0 :(得分:6)
尝试在 之后重新加载表格数据 ,您已完成提取并填写数据数组。此外,清除数据阵列一旦您有查询数据。
例如,在您的refreshResults
方法中,将self.resultsTable.reloadData()
移到for
循环之外:
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()