将数据分配到tableview时出现问题-Swift
虽然我将数据加载到函数中的gifsa字符串数组中,但在tableView中看不到gifsa数据。gifsa数据不显示在tableView中。数据正在veriCek()函数中加载数据。但是,它不会将数据加载到单元格中的imageView中。有什么问题将数据分配到tableview时出现问题-Swift,swift,Swift,虽然我将数据加载到函数中的gifsa字符串数组中,但在tableView中看不到gifsa数据。gifsa数据不显示在tableView中。数据正在veriCek()函数中加载数据。但是,它不会将数据加载到单元格中的imageView中。有什么问题 class NewDegisimController: UITableViewController { var gifsa: [String] = [] override func numberOfSections(in tableVie
class NewDegisimController: UITableViewController {
var gifsa: [String] = []
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gifsa.count
}
override func viewDidLoad() {
super.viewDidLoad()
veriCek()
}
func veriCek(){
let client = SQLClient.sharedInstance()!
client.connect("...", username: "...", password: "...", database: "...") { success in
client.execute("SELECT ... FROM ...", completion: { (_ results: ([Any]?)) in
for table in results as! [[[String:AnyObject]]] {
for row in table {
for (_, value) in row {
if let intVal = value as? String {
self.gifsa.append(String(intVal))
}} }}
client.disconnect()
}) }
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
let model = models[indexPath.row]
do {
print("gifsaas",self.gifsa)
let url = URL(string: self.gifsa[indexPath.row])
let data = try Data(contentsOf: url!)
cell.imageView?.image = UIImage(data: data)
cell.textLabel?.text = model.title
}
catch{
print(error)
}
return cell
}
你需要重新加载
for table in results as! [[[String:AnyObject]]] {
for row in table {
for (_, value) in row {
if let intVal = value as? String {
self.gifsa.append(String(intVal))
}} }}
DispatchQueue.main.async {
self.tableView.reloadData()
}
你只需要打个电话
self.tableView.reload()
在您的
veriCek()
函数中,您已经设置好了客户端。execute是一个异步调用。加载数据后,您必须在完成处理程序中添加self.tabelView.reloadData()
。我没有想到这一点,谢谢:)@rickymo,但是强烈建议您不要在cellForRow
中同步和取消缓存加载图像数据。不要那样做。这将导致非常糟糕的用户体验。为什么要用string(intVal)
从字符串创建字符串?变量名很容易引起误解。