将 UITableView 挂接到 JSON API
Hooking UITableView to a JSON API
我对 Swift 很陌生,我正在尝试以最佳方式将它连接到 UITableView。我决定使用SwiftyJSON,这看起来很简单。我在 JSON 对象中的对象可能如下所示:
{
"id": "146",
"title": "Esports site Streak provides prize-heavy alternative to traditional betting",
"url": "http://www.dailydot.com/esports/streak-counter-strike-vulcun-betting/",
"image_url": "//cdn0.dailydot.com/cache/bb/cc/bbccc49d8271f2f3ed4c40b45c0fe0c0.jpg",
"date": "2015-04-10 22:07:00",
"news_text": "test teeeext",
"referer_img": "1"
}
到目前为止,我已经开始创建一个循环,该循环通过 viewDidLoad
中的所有循环创建循环
for (key: String, subJson: JSON) in jsonArray {
println(subJson)
}
然后我为所有新闻创建了一个 class,如下所示:
class News {
var id: Int!
var title: NSString!
var link: NSString!
var imageLink: NSString!
var summary: NSString!
var date:NSString!
init(id: Int, title:NSString, link: NSString, imageLink:NSString, summary: NSString, date:NSString) {
self.id = id
self.title = title
self.link = link
self.imageLink = imageLink
self.summary = summary
self.date = date
}
}
但是我不确定这是否是创建它的最佳方法?我的下一步是将它连接到 UITableView 吗?
这应该让您知道该怎么做。您需要先创建一个数组。然后,您将创建一个 News 对象并将其逐个添加到该数组。然后,您将为 tableView 的数据源使用该数组。在您的 cellForRowAtIndexPath 中,您将读入 News 对象并显示它们的数据。
var arrayNews = Array<News>()
self.tableView.dataSource = self
for (key: String, subJson: JSON) in jsonArray {
// Create an object and parse your JSON one by one to append it to your array
var newNewsObject = News()
id: = //
title: = //
link = //
imageLink = //
summary = //
date = //
arrayNews.append(newNewsObject)
}
self.tableView.reloadData() // This will read in your arrayNews array
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("NewsCell") as! NewsCell
let newsObject = self.arrayNews[indexPath.row] // Assuming this is 1 section
// set all of your details here
cell.labelBlahBlah.text = something
return cell
}
我对 Swift 很陌生,我正在尝试以最佳方式将它连接到 UITableView。我决定使用SwiftyJSON,这看起来很简单。我在 JSON 对象中的对象可能如下所示:
{
"id": "146",
"title": "Esports site Streak provides prize-heavy alternative to traditional betting",
"url": "http://www.dailydot.com/esports/streak-counter-strike-vulcun-betting/",
"image_url": "//cdn0.dailydot.com/cache/bb/cc/bbccc49d8271f2f3ed4c40b45c0fe0c0.jpg",
"date": "2015-04-10 22:07:00",
"news_text": "test teeeext",
"referer_img": "1"
}
到目前为止,我已经开始创建一个循环,该循环通过 viewDidLoad
中的所有循环创建循环for (key: String, subJson: JSON) in jsonArray {
println(subJson)
}
然后我为所有新闻创建了一个 class,如下所示:
class News {
var id: Int!
var title: NSString!
var link: NSString!
var imageLink: NSString!
var summary: NSString!
var date:NSString!
init(id: Int, title:NSString, link: NSString, imageLink:NSString, summary: NSString, date:NSString) {
self.id = id
self.title = title
self.link = link
self.imageLink = imageLink
self.summary = summary
self.date = date
}
}
但是我不确定这是否是创建它的最佳方法?我的下一步是将它连接到 UITableView 吗?
这应该让您知道该怎么做。您需要先创建一个数组。然后,您将创建一个 News 对象并将其逐个添加到该数组。然后,您将为 tableView 的数据源使用该数组。在您的 cellForRowAtIndexPath 中,您将读入 News 对象并显示它们的数据。
var arrayNews = Array<News>()
self.tableView.dataSource = self
for (key: String, subJson: JSON) in jsonArray {
// Create an object and parse your JSON one by one to append it to your array
var newNewsObject = News()
id: = //
title: = //
link = //
imageLink = //
summary = //
date = //
arrayNews.append(newNewsObject)
}
self.tableView.reloadData() // This will read in your arrayNews array
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("NewsCell") as! NewsCell
let newsObject = self.arrayNews[indexPath.row] // Assuming this is 1 section
// set all of your details here
cell.labelBlahBlah.text = something
return cell
}