gpt4 book ai didi

ios - TableView 未显示值

转载 作者:行者123 更新时间:2023-11-28 15:40:52 24 4
gpt4 key购买 nike

我想给数组赋值并在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com