gpt4 book ai didi

php - 下拉刷新不刷新 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 08:38:15 25 4
gpt4 key购买 nike

我一直在尝试对我的 TableView 实现拉动刷新以从服务器刷新 JSON 数据。在我在调试区域屏幕上尝试过的内容上,它向我显示了数据重新加载,但是当我对服务器上的 PHP 文件进行更改时,单元格标签和图像不会刷新....我正在使用下面的代码:

    import UIKit

class JSONData: UITableViewController {

var newsList = [News]()

override func viewDidLoad() {
super.viewDidLoad()

self.loadJSONDATA()

}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.

return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return newsList.count


}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! NewsStrings

let s = newsList[indexPath.row] as News

cell.labellName.text = s.newsName
cell.labelDesc.text = s.newsDesc
cell.labelDate.text = s.newsDate
cell.imgvImage.image = UIImage(named: "BgIcon.jpg")

if let img = UIImage(data: s.newsImage)
{
cell.imgvImage.image = img
}else
{
self.loadImages(s, indexPath: indexPath)
}



return cell

}


///////////////// JSON DATA ////////////////////////


func loadJSONDATA()
{
let urlString = "http://example.com/folder/JSON.php"

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate:nil, delegateQueue: nil)

if let url = NSURL(string: urlString)
{

let request = NSURLRequest(URL: url)

let taskData = session.dataTaskWithRequest(request, completionHandler: {

(data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in


if (data != nil)
{

var parseError:NSError?
let parsedNews = (try! NSJSONSerialization.JSONObjectWithData(data!, options: [])) as! NSDictionary

//print("JSON Data \n \(parsedNews)")

if let news:AnyObject = parsedNews["News"]
{
self.parseJSON(news)
}


} else

{



}

})

taskData.resume()


}

}



/////////// LODING JSON DATA //////////////

func parseJSON(jsonData:AnyObject)
{

if let newsData = jsonData as? [[NSObject:AnyObject]]
{

var news:News


for s in newsData {

news = News()


if let sId:AnyObject = s["NewsID"]
{

if let NewsID = sId as? String

{
print("News id = \(NewsID)")

}

}


if let sn:AnyObject = s["newsName"]
{

if let newsName = sn as? String

{

news.newsName = newsName
//println("Store id = \(storeName)")

}

}

if let sn:AnyObject = s["newsDate"]
{

if let newsDate = sn as? String

{

news.newsDate = newsDate
//println("Store id = \(storeName)")

}

}


if let sn:AnyObject = s["newsIcon"]
{

if let newsIcon = sn as? String

{

news.newsImageName = newsIcon
//println("News Icon = \(newsIcon)")

}

}


if let sn:AnyObject = s["newsDesc"]
{

if let newsIcon = sn as? String

{

news.newsDesc = newsIcon

}

}


newsList += [news]


}


NSOperationQueue.mainQueue().addOperationWithBlock(){

UIApplication.sharedApplication().networkActivityIndicatorVisible = false
self.tableView.reloadData()
}
}
}


/////////// LODING IMAGES FROM JSON DATA //////////////

func loadImages(news:News, indexPath:NSIndexPath)
{

let urlString = news.newsImageName


let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate:nil, delegateQueue: nil)


if let url = NSURL(string: urlString)
{
let request = NSURLRequest(URL: url)

let taskData = session.dataTaskWithRequest(request, completionHandler: {

(data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in


if (data != nil)
{

news.newsImage = data!
let image = UIImage(data: data!)

dispatch_async(dispatch_get_main_queue(),{

if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? NewsStrings {
cell.imgvImage.image = image
}
})



} else

{


}

})

taskData.resume()
}
}

}

最佳答案

您可以像下面的代码一样在您的表格 View 中添加拉动刷新,

var refreshControl = UIRefreshControl()

在 View 中加载,

self.refreshControl.addTarget(self, action: Selector("loadJSONDATA"), forControlEvents: UIControlEvents.ValueChanged)
self.addSubview(self.refreshControl) // OR self.myTable?.addSubview(self.refreshControl)

然后在self.parseJSON(news)之后添加这一行

self.refreshControl.endRefreshing()

这将对您有所帮助。 :)

关于php - 下拉刷新不刷新 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com