作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个自定义的 UITableview 单元格中有 3 个标签,我正在尝试传递我从带有 Alamofire 的 api 获得的 json 数据,但我很难理解如何将返回的 json 推送到 tableview 中。任何帮助将不胜感激。代码如下:
import UIKit
import Parse
import Alamofire
class LeagueTableController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "https://api.import.io/store/connector/88c66c----9b01-6bd2bb--d/_query?input=webpage/url:----") .responseJSON { response in // 1
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)")
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
return cell
}
}
返回的json是这样的:
{
connectorGuid = "88c66cb4-e64f-4316-9b01-6bd2bb2d762d";
connectorVersionGuid = "8aedfe43-948a-4559-b279-d3c3c28047a4";
cookies = (
);
offset = 0;
outputProperties = (
{
name = team;
type = URL;
},
{
name = played;
type = DOUBLE;
},
{
name = points;
type = DOUBLE;
}
);
pageUrl = "http://www.extratime.ie/leagues/2024/100/premier-division/";
results = (
{
played = 9;
"played/_source" = 9;
points = 22;
"points/_source" = 22;
team = "http://www.extratime.ie/squads/17/";
"team/_source" = "/squads/17/";
"team/_text" = Dundalk;
},
{
played = 9;
"played/_source" = 9;
points = 20;
"points/_source" = 20;
team = "http://www.extratime.ie/squads/7/";
"team/_source" = "/squads/7/";
"team/_text" = "Derry City";
},
{
played = 9;
"played/_source" = 9;
points = 17;
"points/_source" = 17;
team = "http://www.extratime.ie/squads/100504/";
"team/_source" = "/squads/100504/";
"team/_text" = "Galway United FC";
},
{
played = 9;
"played/_source" = 9;
points = 16;
"points/_source" = 16;
team = "http://www.extratime.ie/squads/29/";
"team/_source" = "/squads/29/";
"team/_text" = "St. Patrick's Ath";
},
{
played = 8;
"played/_source" = 8;
points = 15;
"points/_source" = 15;
team = "http://www.extratime.ie/squads/30/";
"team/_source" = "/squads/30/";
"team/_text" = "Cork City";
},
{
played = 8;
"played/_source" = 8;
points = 15;
"points/_source" = 15;
team = "http://www.extratime.ie/squads/3/";
"team/_source" = "/squads/3/";
"team/_text" = "Shamrock Rovers";
},
{
played = 9;
"played/_source" = 9;
points = 10;
"points/_source" = 10;
team = "http://www.extratime.ie/squads/13/";
"team/_source" = "/squads/13/";
"team/_text" = "Finn Harps";
},
{
played = 9;
"played/_source" = 9;
points = 10;
"points/_source" = 10;
team = "http://www.extratime.ie/squads/2/";
"team/_source" = "/squads/2/";
"team/_text" = Bohemians;
},
{
played = 9;
"played/_source" = 9;
points = 7;
"points/_source" = 7;
team = "http://www.extratime.ie/squads/8/";
"team/_source" = "/squads/8/";
"team/_text" = "Sligo Rovers";
},
{
played = 9;
"played/_source" = 9;
points = 7;
"points/_source" = 7;
team = "http://www.extratime.ie/squads/6/";
"team/_source" = "/squads/6/";
"team/_text" = "Bray Wanderers";
},
{
played = 9;
"played/_source" = 9;
points = 5;
"points/_source" = 5;
team = "http://www.extratime.ie/squads/109/";
"team/_source" = "/squads/109/";
"team/_text" = "Wexford Youths";
},
{
played = 9;
"played/_source" = 9;
points = 5;
"points/_source" = 5;
team = "http://www.extratime.ie/squads/15/";
"team/_source" = "/squads/15/";
"team/_text" = "Longford Town";
}
);
}我正在尝试将“played”、“points”和“team/_text”结果推送到每个标签。
最佳答案
由于问题非常广泛,并且没有具体说明这里的问题到底是什么,一般步骤是:
1) 将您的 json 映射到字典/nsdictionary。假设您发布的 JSON 片段是以下格式 [{}] 的 JSONArray block ,您需要做的就是:
var arrayOfDictionaries:NSArray = NSJSONSerialization.JSONObjectWithData(yourData, options: nil, error: nil) as! NSArray
其中 yourData 变量是从网络下载的转换为 NSData 格式的数据
2) 在自定义 tableViewCell 中创建这三个标签的导出
3) 对于每个单元格,在其中设置这些标签
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
方法如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:YourCustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as YourCustomCell
cell.firstLabel.text = yourDictionary["played"]
cell.secondLabel.text = yourDictionary["points"]
cell.thirdLabel.text = yourDictionary["team"]
return cell
}
4)我想您将需要更多的单元格,而不仅仅是一个单元格,然后在数组中存储许多字典并访问每个元素,如下所示:
cell.firstLabel.text = arrayOfDictionaries[indexPath.row]["played"]
关于ios - 如何将 Json 数据推送到 UITableView 中的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36792073/
我是一名优秀的程序员,十分优秀!