我在自定义的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";
}
);
} 我试图将“play”,“points”和“team / _text”结果推送到每个标签。
答案 0 :(得分:1)
由于问题非常广泛,并没有详细说明问题究竟是什么,所以一般步骤是:
1)将你的json映射到dictionary / nsdictionary。假设您发布的JSON片段是以下格式[{}]的JSONArray块,您需要做的就是:
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"]
答案 1 :(得分:0)
您需要创建UITableViewCell
的子类,比如MyTableViewCell
并添加名为JSON
的属性。
现在,由于您可能正在使用Interface Builder来定义您的单元格及其重用标识符(“Cell”),请将该单元格的类设置为新创建的MyTableViewCell
并将标签连接到某些IBOutlets
在你新定义的类中。
然后,当您调用'dequeueReusableCellWithIdentifier'时,将单元格强制转换为MyTableViewCell
并将其JSON
属性设置为您希望在单元格中拥有的值。
您可能希望对更改做出反应,因此请添加didSet
属性观察者。
var JSON:[String: AnyObject] = [String: AnyObject]() {
didSet {
print("populate your labels with your new data");
}
}
答案 2 :(得分:0)
首先,您应该创建一个包含3个要保存的属性的模型,例如:
=IFERROR(INDEX(Data!$G:$G,MIN(IF(Data!$E$2:$E$5000>=TODAY()-7),ROW($2:$5000)),1,"")
在您的LeagueTableController中,创建一个数组来保存数据并将其显示到tableView:
DECLARE @i int = 0
WHILE @i < 9 --replace 9 as necessary - probably rowcount / 10
BEGIN
SELECT (Q._Row / 10) as myGroup, Q.[Type], Q.[Message] FROM
(
SELECT ROW_NUMBER() OVER(ORDER BY AuditLogId DESC) AS _Row,
[Type], [Message]
FROM tblAuditLog
) Q
WHERE (Q._Row / 10) = 0
FOR XML AUTO
--SAVE THAT XML somewhere
set @I = @I + 1
END
配置tableView以显示数据:
class Data {
var team = ""
var point = 0
var teamText = ""
init(fromJSON json: NSDictionary) {
team = json["team"] as! String
point = json["points"] as! Int
teamText = json["team/_text"] as! String
}
}
最后,将您的响应json解析为我们的数据数组以显示在tableView
上var data = [Data]()
希望这有帮助!