ios - TableView 未显示值

标签 ios arrays iphone swift

我想给数组赋值并在 TableView 中显示该值.. 我正在使用 webservice 代码通过 webservice 获取值。 值越来越完美,我使用 print 语句进行测试,它完美地显示了该值。但是当我将该值分配给 TableView 时,它什么也没显示。这是我的代码。请帮帮我

let con = "http://192.168.10.10/Hand.svc/";
var list = [String]()
var answer:String = ""

override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: con+"getlocation?email=saim@gmail.com")
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error != nil
        {
            print("Error")
        }
        else
        {
            if let content = data
            {
                do
                {
                    let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
                    let asd = myJson.value(forKey: "getlocationResult")
                    self.answer = String(describing: asd!)
                    var mystring =  self.answer.components(separatedBy:"=")
                       let size = mystring.count
                    var i = 0
                    while (i < size-1 )
                    {
                        let st1 = mystring[i+1]
                         self.list = st1.components(separatedBy: ";")
                        print (self.list[0])
                        i += 1
                    }


                }
                catch
                {

                }
            }
        }

    }
    task.resume()


    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return(list.count)
}


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.text = list[indexPath.row]
        return(cell)
    }
}

最佳答案

DispatchQueue.main.async - 用于更新 UI,因为网络调用是后台队列。

 do {
     let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
       let asd = myJson.value(forKey: "getlocationResult")
       self.answer = String(describing: asd!)
       var mystring =  self.answer.components(separatedBy:"=")
       let size = mystring.count
        var i = 0
       while (i < size-1 )
         {
            let st1 = mystring[i+1]
            self.list = st1.components(separatedBy: ";")
            print (self.list[0])
            i += 1
           }
         DispatchQueue.main.async(execute: { () -> Void in
            self.yourTableViewName.reloadData()  
          })
   }
   catch
   {
   }

瓦迪安评论;

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return list.count 
}


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.text = list[indexPath.row]
        return cell
    }
}

关于ios - TableView 未显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43699832/

相关文章:

javascript - 在 JavaScript 中使用 == 的相等对象字符串

iphone - 在这种情况下,可以一次又一次地重新分配窗口的 rootviewcontroller 吗?

ios - 获取 Apple 智能电池盒的电池电量

ios - 如果我将手指从我的节点移开并进入 GameView,为什么我的节点会停止旋转?

ios - 如何使用 HOKO Links 实现延迟深度链接?

ios - Swift IOS 使用 AVFoundation 录制视频和音频

javascript - 使用 AngularJS 编辑数组中的非字符串对象

javascript - 如何遍历具有相同前缀的id

iphone - 什么是像 UIKeyboardFrameEndUserInfoKey 这样的常量,用于 iphone 中的附加日文键盘面板?

ios - 如何从带有多个主题标签的 instagram 获取图像?