【问题标题】:TableView not displaying text with JSON data from API callTableView 不显示来自 API 调用的带有 JSON 数据的文本
【发布时间】:2017-03-26 01:20:31
【问题描述】:

我使用 AlamofireSwiftyJson 连接到 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 上显示结果。 谁能帮我?谢谢!

【问题讨论】:

  • numberOfRowsInSection cellForRowAt 这个方法被调用了吗?
  • 1.您是否将 ViewController 设置为情节提要中 tableView 的委托和数据源? 2. 你设置了 override numberOfSectionsInTableView: 返回 1 吗?
  • 您必须在主队列中调用reloadData
  • @W.K.S 如果您只有一个部分,则无需实现numberOfSectionsInTableView。这是默认设置。
  • @rmaddy 对不起,我的措辞很糟糕:我的意思是 OP 覆盖了该方法并返回 0,因为他认为没有部分。这是我新手时犯的一个错误。

标签: ios json swift uitableview


【解决方案1】:

你需要像这样实现UITableViewDataSourceUITableViewDelegate

class ViewController:UIViewController, UITableViewDataSource, UITableViewDelegate 

viewDidLoad 中,您需要像这样将DataSourceDelegate 分配给您的tableView

override func viewDidLoad() {
    super.viewDidLoad()
    tblJSON.dataSource = self
    tblJSON.delegate = self
    getRequest()
}

【讨论】:

  • 也许你知道,为什么 TableView 只显示“名称”,尽管我在代码中写了:cell.textLabel?.text = dict["name"] as?细绳; cell.detailTextLabel?.text = dict["email"] as?字符串
  • print(cell.detailTextLabel) 在 cellForRow 中添加这一行并检查它是否不为零
  • xCode 告诉我它是 nil
  • 检查该标签的插座是否连接
  • 插座不需要。因为我使用默认标签(open var textLabel: UILabel? { get } // 默认为 nil。如有必要,将创建标签。)我的标签已创建,但我的 detailLabel 未创建。我不知道为什么。我不在情节提要中使用 UILabel。
猜你喜欢
  • 2015-10-05
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 2019-12-28
  • 2019-05-06
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多