ios - swift 3 : Custom Cell not displaying data

标签 ios swift3 tableview

我从 php 接收一个 JSON 数组并尝试将数据传递到 TableView 。但是,我的 tableview 不显示数据。

class test1ViewController: UIViewController  , UITableViewDataSource, UITableViewDelegate {


var TableData:Array< String > = Array < String >()



public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

    return TableData.count

}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! testTableViewCell

    cell.mylabel1.text = TableData[indexPath.row]
    return cell

}

 func get_data_from_url(_ link:String)
{
    let url:URL = URL(string: link)!
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "GET"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData


    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (
        data, response, error) in

        guard let _:Data = data, let _:URLResponse = response  , error == nil else {

            return
        }

        print(data!)
        self.extract_json(data!)


    })

    task.resume()

}

func extract_json(_ data: Data)
{


    let json: Any?

    do
    {
        json = try JSONSerialization.jsonObject(with: data, options: [])
      //  print(json!)

    }
    catch
    {
        return
    }

    guard let data_list = json as? NSArray  else
    {
        return


    }

    if let countries_list = json as? NSArray
    {
        for i in 0 ..< data_list.count
        {
            if let country_obj = countries_list[i] as? NSDictionary
            {

                if let name = country_obj["name"] as? String
                {
                    if let age = country_obj["age"] as? String
                    {
                        TableData.append(name + age)




                    }
                }
            }
        }
    }




}

当数组给出初始值时 animals = ["hinno", "ali", "khalil"], 这些值出现在自定义单元格中,但是当我从服务器获取数据并进行 json 转换时,什么也没有出现。

最佳答案

每当您对数组进行更改时,tableview.reloadData()。

关于ios - swift 3 : Custom Cell not displaying data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47575110/

相关文章:

ios - 尝试创建委托(delegate)时获取 nil

ios - CloudKit CKError "Invalid Arguments"与 "Bad sync continuation data"

ios - iPad 删除后记住相机权限 - 如何清除?

ios - 如何使用 Google Maps ios SDK 跟踪用户的位置并显示行进的路径

ios - 将数据从 Map Annotation 传递到详细 View Controller SWIFT 3

ios - UITableView 未显示所有部分

ios - Swift-CustomCell 中 ScrollView 上的按钮相互重叠

ios - 嵌入式 View Controller (Swift 3)中永远不会调用 addObserver 选择器函数

ios - 如何在 Swift 3 中隐藏 TabBarController 上的自定义按钮?

swift - 更改 tableView 中单元格的 editingStyle 背景颜色和标签