我的刷新功能实际上有效,但它不会重新加载第一个单元格的数据。我的意思是,如果我刷新表格细胞混合。第一个细胞不会改变,但新细胞应该是新出现在下面细胞中显示的第一个细胞。如果我关闭并打开应用程序,我可以按顺序访问数据,但如果我使用pull刷新,则无法按顺序刷新数据。
var page : Int = 1
var refresher: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
refresher.addTarget(self, action: #selector(newPollsTableViewController.refresh), forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
data_request()
}
这是我的data_request()函数:
func data_request(){
let user_id = Data[0].valueForKey("user_id") as? String!
let url:NSURL = NSURL(string: "http://..../perspective/\(user_id!)/page/"+"\(page)" )!
let session = NSURLSession.sharedSession()
self.page = self.page + 1
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let task = session.dataTaskWithRequest(request) {
(let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
var json: AnyObject?
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
}
catch {
return
}
guard let data_array = json as? NSArray else {
return
}
for i in 0..<data_array.count
{
if let add = data_array[i] as? NSDictionary
{
self.obj.append(Obj(data:add))
}
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
task.resume()
}
当我拉动表格时,我调用了我的刷新功能。
func refresh() {
self.page = 1
data_request()
self.refresher.endRefreshing()
}
每次刷新表时,它都会输入data_request()
func,但不会更新表格。
答案 0 :(得分:0)
数据源发生了奇怪的事情,请尝试修改您的caching policy
自:
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
要:
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
答案 1 :(得分:0)
我在self.obj.removeAll()
self.obj.append(Obj(data:add))
解决了这个问题