所以,我试图从 sqlserver 获取数据到我的 tableview 但我似乎没有得到它我遵循了一个教程并且很确定我没有做错我也在使用自定义单元格,唯一有效的是连接到 sql 因为它在输出日志中打印数据我对 swift 和 xcode 真的很陌生,我不知道我在做什么。
无论如何,这是我的代码:
TableCell.swift
import UIKit
class TableCell: UITableViewCell {
@IBOutlet weak var idtext: UILabel!
@IBOutlet weak var usernametxt: UILabel!
func setText(text: textstuff) {
idtext.text = text.id
usernametxt.text = text.username
}
}
View Controller import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableview: UITableView!
var taable: [textstuff] = []
override func viewDidLoad() {
super.viewDidLoad()
taable = createArray()
//Adding the observer for error and to receive the message
}
func createArray() -> [textstuff]{
var temptext: [textstuff] = []
NotificationCenter.default.addObserver(self, selector: #selector(error(_:)), name: NSNotification.Name.SQLClientError, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(message(_:)), name: NSNotification.Name.SQLClientMessage, object: nil)
let client = SQLClient.sharedInstance()!
client.connect("x.x.x.x", username: "xxxxxx", password: "xxxxxx", database: "xxxxxx") { success in
client.execute("SELECT username FROM Supervisors", completion: { (_ results: ([Any]?)) in
for table in results as! [[[String:AnyObject]]] {
for row in table {
for (columnName, value) in row {
print("\(columnName) = \(value)")
let newVal = value as? String ?? ""
let userandid = textstuff(id: columnName, username: newVal)
temptext.append(userandid)
}
}
}
client.disconnect()
})
}
return temptext
}
@objc func error(_ notification: Notification?) {
let code = notification?.userInfo?[SQLClientCodeKey] as? NSNumber
let message = notification?.userInfo?[SQLClientMessageKey] as? String
let severity = notification?.userInfo?[SQLClientSeverityKey] as? NSNumber
if let code = code, let severity = severity {
print("Error #\(code): \(message ?? "") (Severity \(severity))")
}
}
@objc func message(_ notification: Notification?) {
let message = notification?.userInfo?[SQLClientMessageKey] as? String
print("Message: \(message ?? "")")
}
}
extension ViewController:UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taable.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let text = taable[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "TextCell") as! TableCell
cell.setText(text: text)
return cell
}
}
textstuff.swift import Foundation
class textstuff{
var id: String
var username: String
init(id: String, username: String) {
self.id = id
self.username = username
}
}
我真的很感激任何帮助!编辑
我尝试打印
taable.count
正如预期的那样,它打印了 0
最佳答案
你陷入了异步陷阱。您不能从异步任务中返回某些内容作为方法的返回值。返回的数组将始终为空。
在这种情况下,解决方案非常简单。在完成处理程序结束时,将结果分配给数据源数组并重新加载 TableView 。
替换 viewDidLoad
和
override func viewDidLoad() {
super.viewDidLoad()
createArray()
}
替换 createArray
和func createArray() {
var temptext: [textstuff] = []
NotificationCenter.default.addObserver(self, selector: #selector(error(_:)), name: NSNotification.Name.SQLClientError, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(message(_:)), name: NSNotification.Name.SQLClientMessage, object: nil)
let client = SQLClient.sharedInstance()!
client.connect("x.x.x.x", username: "xxxxxx", password: "xxxxxx", database: "xxxxxx") { success in
client.execute("SELECT username FROM Supervisors", completion: { (_ results: ([Any]?)) in
for table in results as! [[[String:AnyObject]]] {
for row in table {
for (columnName, value) in row {
print("\(columnName) = \(value)")
let newVal = value as? String ?? ""
let userandid = textstuff(id: columnName, username: newVal)
temptext.append(userandid)
}
}
}
self.taable = temptext
DispatchQueue.main.async {
self.tableview.reloadData()
}
client.disconnect()
})
}
}
并且请遵守命名约定,并以大写字母 (Textstuff
) 命名结构和类
关于ios - tableview swift中的行为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62900450/