【发布时间】:2017-03-26 01:20:31
【问题描述】:
我使用 Alamofire 和 SwiftyJson 连接到 API。
我收到了这个 JSON 返回:
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
这是我的 ViewController 代码:
导入 UIKit 进口阿拉莫火 导入 SwiftyJSON 类视图控制器:UIViewController { @IBOutlet 弱变量 tblJSON:UITableView! var arrRes = [[String : AnyObject]]() 覆盖 func viewDidLoad() { super.viewDidLoad() 获取请求() } 覆盖 func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // 处理所有可以重新创建的资源。 } 函数 getRequest() { Alamofire.request("http://api.androidhive.info/contacts").responseJSON { (responseData) -> Void in if ((responseData.result.value) != nil) { 让 swiftyJsonVar = JSON(responseData.result.value!) 如果让 resData = swiftyJsonVar["contacts"].arrayObject { self.arrRes = resData as! [[字符串:任何对象]] } 如果 self.arrRes.count > 0 { self.tblJSON.reloadData() } } } } //MARK: 表视图 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 返回 arrRes.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 让单元格:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell")! var dict = arrRes[indexPath.row] cell.textLabel?.text = dict["name"] as?细绳 cell.detailTextLabel?.text = dict["email"] as?细绳 返回单元格 } }我无法在 UITableView 上显示结果。
谁能帮我?谢谢!
【问题讨论】:
-
numberOfRowsInSectioncellForRowAt这个方法被调用了吗? -
1.您是否将
ViewController设置为情节提要中 tableView 的委托和数据源? 2. 你设置了 overridenumberOfSectionsInTableView:返回 1 吗? -
您必须在主队列中调用
reloadData。 -
@W.K.S 如果您只有一个部分,则无需实现
numberOfSectionsInTableView。这是默认设置。 -
@rmaddy 对不起,我的措辞很糟糕:我的意思是 OP 覆盖了该方法并返回 0,因为他认为没有部分。这是我新手时犯的一个错误。
标签: ios json swift uitableview