我正在尝试使用网络调用中的数据在UItableview中显示为单元名称
这是我当前的视图控制器
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController, UITableViewDataSource{
var articles = [Article]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//get data from network call
loaddata()
//end view did load
}
func loaddata(){
Alamofire.request(.GET, "http://ip-address/test.json")
.responseJSON { response in
// print(response.request) // original URL request
// print(response.response) // URL response
//print(response.data) // server data
//print(response.result) // result of response serialization
/*if let JSON = response.result.value {
print("JSON: \(JSON)")
}*/
//get json from response data
let json = JSON(data: response.data!)
//print(json)
//for loop over json and write all article titles articles array
for (key, subJson) in json["Articles"] {
if let author = subJson["title"].string {
let artTitle = Article(name: author)
self.articles.append(artTitle!)
}
/*if let content = subJson["content"].string {
// self.Content.append(content)
}*/
}
// print("\(self.titles)")
//print("\(self.Content[0])")
//print(self.articles)
//set variable to articles number 6 to check append worked
let name = self.articles[6].name
//print varibale name to check
print("\(name)")
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//let num = articles.count
// print(num)
//return number of rows
return articles.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
//let (artTitle) = articles[indexPath.row]
// Fetches the appropriate article for the data source layout.
let article = articles[indexPath.row]
//set cell text label to article name
cell.textLabel?.text = article.name
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//end of class
}
这是Article.swift文件
class Article {
// MARK: Properties
var name: String
// MARK: Initialization
init?(name: String) {
// Initialize stored properties.
self.name = name
// Initialization should fail if there is no name or if the rating is negative.
if name.isEmpty {
return nil
}
}
}
我想我差不多了,到目前为止我能做的就是
我无法将其设置为单元格名称
这是因为在加载数据之前加载了UItableview,因此该文章在那时是空的吗?
你能否指出我最好的做法,比如这个/最好的设计模式,并帮助我加载数据
答案 0 :(得分:1)
从请求中获取数据后,在主线程上的tableView上调用reloadData,如此。
dispatch_async(dispatch_get_main_queue(), {
[unowned self] in
self.tableView.reloadData()
})
这将使tableView刷新其所有内容,然后您的数据将显示出来。
答案 1 :(得分:0)
Alamofire.request(.GET, Urls.menu).responseJSON { response in
if let jsonData = response.data {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
let json = JSON(data: jsonData)
//for loop over json and write all article titles articles array
newArticles = [Articles]()
for (key, subJson) in json["Articles"] {
if let author = subJson["title"].string {
if let artTitle = Article(name: author) {
newArticles.append(artTitle)
}
}
}
dispatch_async(dispatch_get_main_queue()) {
self.articles += newArticles
tableView.reloadData()
/*if let content = subJson["content"].string {
// self.Content.append(content)
}*/
}
// print("\(self.titles)")
//print("\(self.Content[0])")
//print(self.articles)
//set variable to articles number 6 to check append worked
let name = self.articles[6].name
//print varibale name to check
print("\(name)")
}
}
}