简体   繁体   English

表格视图中的SWIFT数据未显示

[英]SWIFT Data in Table View not showing

I need help on how to solve this problem. 我需要有关如何解决此问题的帮助。

I tried to fetch data using json but when I tried to view in Table View its not showing. 我试图使用json来获取数据,但是当我尝试在Table View中查看时,它没有显示。

I used the code below to test if table view is working and it works! 我使用下面的代码测试表视图是否正常工作!

// self.clientList = ["Mango", "Banana", "Orange", "Guava", "Grapes"] // self.clientList = [“芒果”,“香蕉”,“橙色”,“番石榴”,“葡萄”]

I used the code below to test if there's data returned from json. 我使用下面的代码测试json是否返回了数据。 Still it works. 仍然有效。

for item in jsonClientList {

  let firstName = item["firstName"]
   //Printing is working
  print(firstName  as! String)

}

Line not working! 线不工作! I dont know why. 我不知道为什么。 Its inside of loop but to data upon loading the table view. 它在循环内部,但在加载表视图时才对数据进行循环。

Thanks in advance. 提前致谢。

self.clientList.append(firstName as! String) self.clientList.append(firstName as!String)

//---------------------------------


var clientList:[String] = []


func tableView(tableView: UITableView, numberOfRowsInSection section:    Int) -> Int {
    return self.clientList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "tblClientList")
    cell.textLabel?.text = self.clientList[indexPath.row]
    return cell
}


internal func jsonParser() -> Void{

    //-------------------------

    let postEndpoint: String = "http://domain.com/client"
    let url = NSURL(string: postEndpoint)

    let session = NSURLSession.sharedSession()
    session.dataTaskWithURL(url!, completionHandler:
        {
            (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in

            do{
                let ipString = NSString(data:data!, encoding: NSUTF8StringEncoding)
                if (ipString != nil) {
                    let jsonClientList = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray

                        for item in jsonClientList {
                            let firstName = item["firstName"]
//I tried to print the data from json and its working!
                         print(firstName  as! String)
//Line not working
//I tried to insert the firstName to clientList array
self.clientList.append(firstName  as! String)

                        }
                 }

                    //If I use this its working
                    //  self.clientList = ["Mango", "Banana", "Orange", "Guava", "Grapes"]

                }
            } catch{
                print("Something bad happed!")
            }
        }

        ).resume()
    //--------------


}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    jsonParser()

}
//---------------------------------

you forget to refresh the new data to show in table, do like 您忘记刷新新数据以显示在表格中,就像

 self.clientList.append(firstName  as! String)
 }
dispatch_async(dispatch_get_main_queue()) 
self.yourtableViewname.reloadData()
} 

As mentioned in the other answer the issue is that the table view needs to be reloaded. 如另一个答案中所述,问题是表视图需要重新加载。

In Swift there is a more convenient way to populate the data source array without repeat loop using the map function. 在Swift中,有一种更方便的方法来填充数据源数组,而无需使用map函数进行重复循环。

It assumes – like in the question – that all dictionaries in jsonClientList contain a key firstName . 像问题中一样,它假设jsonClientList中的所有字典jsonClientList包含一个键firstName

tableView is the name of the UITableView instance. tableViewUITableView实例的名称。

...
    let jsonClientList = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [[String:AnyObject]]

    self.clientList = jsonClientList.map{ $0["firstName"] as! String }
    dispatch_async(dispatch_get_main_queue()) {
       self.tableView.reloadData()
    }
  }
} catch {
...

Reading the JSON with mutable containers is not needed in this case. 在这种情况下,不需要使用可变容器读取JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM