我正在使用Swift,我试图为我的表添加一个拉动刷新功能,但它不会停止刷新我认为我可能有一个问题我的endrefresher也当它刷新时不会停止当你再次刷新应用程序崩溃时出现NSexception错误。我如何结束刷新并刷新页面,如果再次拉下来避免崩溃? 继承我的代码:
class HomePage: UITableViewController {
var images = [UIImage]()
var titles = [String]()
var imageFile = [PFFile]()
var votingObjects: [PFObject] = []
var objectIds = [""]
var refresher: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
func refresh() {
println(PFUser.currentUser())
println(PFUser.currentUser())
var query = PFQuery(className:"Post")
query.orderByDescending("createdAt")
query.limit = 15
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
println("Successfully retrieved \(objects!.count) scores.")
println(objects!)
for objectRow in objects! {
let object = objectRow as! PFObject
if let objectIds = object["objectId"] as? String {
self.objectIds.append(objectIds)
}
if let title = object["Title"] as? String {
self.titles.append(title)
}
if let imgFile = object["imageFile"] as? PFFile {
self.imageFile.append(imgFile)
}
self.votingObjects.append(object)
self.refresher.endRefreshing()
}
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData() // Updating the tableView on the main thread - important. Do some research on Grand Central Dispatch :)
})
} else {
println(error)
// Error
}
}
}
refresh()
}
答案 0 :(得分:0)
在此处设置刷新器以在完成时调用函数refresh:
refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
所以我们需要实现这个功能:
func refresh(){
println("Refresh")
refresher.endRefreshing()
}
现在将调用此函数并且刷新将停止