Ios 将UITableView挂接到JSON API
我对斯威夫特很陌生,我正试图用最好的方式把它连接到一个UITableView上。我决定使用SwiftyJSON,这似乎很简单。JSON对象中的我的对象可能如下所示:Ios 将UITableView挂接到JSON API,ios,json,uitableview,swift,swifty-json,Ios,Json,Uitableview,Swift,Swifty Json,我对斯威夫特很陌生,我正试图用最好的方式把它连接到一个UITableView上。我决定使用SwiftyJSON,这似乎很简单。JSON对象中的我的对象可能如下所示: { "id": "146", "title": "Esports site Streak provides prize-heavy alternative to traditional betting", "url": "http://www.dailydot.com/esports/streak-count
{
"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 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?这将让您知道该怎么做。您需要先创建一个数组。然后,您将创建一个新闻对象,并将其逐个添加到该数组中。然后将该数组用于tableView的数据源。在cellForRowAtIndexPath中,您将读取新闻对象并显示其数据
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
}
var arrayNews=Array()
self.tableView.dataSource=self
jsonArray中的for(key:String,subson:JSON){
//创建一个对象并逐个解析JSON以将其附加到数组中
var newnewobject=News()
id:=//
标题:=//
链接=//
imageLink=//
摘要=//
日期=//
arrayNews.append(newnewobject)
}
self.tableView.reloadData()//这将在arrayNews数组中读取
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
var cell=tableView.dequeueReusableCellWithIdentifier(“NewsCell”)as!NewsCell
让newobject=self.arrayNews[indexPath.row]//假设这是1节
//在此处设置所有详细信息
cell.labelBlahBlah.text=某物
返回单元
}
只需将从json创建的新闻
对象创建一个数组,并将该数组用作UITableView的模型即可。所以第一个单元格将显示数组的第一个对象,等等。你能用sudo代码或代码片段回答吗?好的,谢谢!!顺便说一下,我将循环浏览2个json文件。一个是最近的新闻,一个是流行新闻。我需要为此创建不同的新闻类,还是只需要创建两个不同的数组并将它们附加到每个字段?如果它们使用相同的字段,则使用相同的类并创建多维数组来存储不同的“节”。数组。然后在cellForRowAtIndexPath中,您将通过该数组破译您的分区。self.arrayNews[section][row]。无法将数组类型的值分配给UITableViewDataSource类型的值我不确定他为什么要将数据源分配给数组。您的UIViewController
应该是dataSource
,因为它是将实现UITableViewDataSource
函数的类(即cellforrowatinexpath
)。还要确保实现numberOfSections
和numberofrowsinssection
(记不清确切的名称)。您应该将tableview的行数设置为数组的count
属性。这不是我想说的重点,但您是正确的。通过委托方法使用数组。委托方法访问该数组。(在可生成代码时并不打算这样做)