gpt4 book ai didi

swift - 将sql中的数据拉取到 TableView

转载 作者:行者123 更新时间:2023-11-30 10:31:26 26 4
gpt4 key购买 nike

我想将从Mssql获取的数据打印到tableview。但我无法将此数据打印到 TableView 中。我要打印的数据是一个邮件地址,一个字符串值。我正在使用mssql数据库。如何将从 msql 获取的数据打印到 tableview?

class MakaleController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var makaleTable: UITableView!
var stringvalue: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
let client = SQLClient.sharedInstance()!
client.connect("asd", username: "asd", password: "asd", database: "asd") { success in
client.execute("SELECT Email FROM and", completion: { (_ results: ([Any]?)) in
for table in results as! [[[String:AnyObject]]] {
for row in table {
for (columnName, value) in row {
print(value)
if let intVal = value as? Int {
self.stringvalue.append(String(intVal))
DispatchQueue.main.async { self.makaleTable.reloadData() }}} }}
client.disconnect()
})
}
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.makaleTable.dequeueReusableCell(withIdentifier: "makalecell") as! UITableViewCell
cell.textLabel?.text = self.stringvalue[indexPath.row]
return cell
}
}

最佳答案

首先,检查您是否已成功将原始值转换为 Int (这就是我添加第二个打印语句的原因),然后在所有循环之后重新加载表(而不是从它们内部加载)正如你所做的那样):

override func viewDidLoad() {
super.viewDidLoad()

let client = SQLClient.sharedInstance()!

client.connect("asd", username: "asd", password: "asd", database: "asd") { success in

client.execute("SELECT Email FROM and", completion: { (_ results: ([Any]?)) in

for table in results as! [[[String:AnyObject]]] {

for row in table {

for (columnName, value) in row {

print(value) // prints correctly as NVARCHAR

if let val = value as? String { // edit: skip casting as Int, go straight to String
stringvalue.append(val)
}

}

}

}

// reload table once after all loops are complete
DispatchQueue.main.async {
self.makaleTable.reloadData()
}

client.disconnect()

})

}

}

关于swift - 将sql中的数据拉取到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59127932/

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