簡體   English   中英

表格視圖未顯示值

[英]Table View is not showing the values

我想將值分配給數組並在表視圖中顯示該值。我正在使用Web服務代碼通過Web服務獲取這些值。 值越來越完美,我使用print語句進行測試,完美地表明了這些值。 但是當我將該值分配給表視圖時,它什么也沒顯示。 這是我的代碼。 請幫幫我

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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM