Swift Can';t保存数据并从闭包中重新加载TableView
我是斯威夫特的新手。我已连接到websocket(使用红蜘蛛),并且我正在等待通过此方法接收文本或事件:Swift Can';t保存数据并从闭包中重新加载TableView,swift,swift5,Swift,Swift5,我是斯威夫特的新手。我已连接到websocket(使用红蜘蛛),并且我正在等待通过此方法接收文本或事件: func websocketDidReceiveMessage(socket: WebSocketClient, text: String) 当我获得一个事件(仅ID)时,我会从另一个viewcontroller触发func fetchInfoAboutInvitations(text:String);此func引用http请求(从服务器获取有关事件的所有信息),此func带有完成: fu
func websocketDidReceiveMessage(socket: WebSocketClient, text: String)
当我获得一个事件(仅ID)时,我会从另一个viewcontroller触发func fetchInfoAboutInvitations(text:String)
;此func引用http请求(从服务器获取有关事件的所有信息),此func带有完成:
func gettingInfoAboutInvitationAfterSocket(invitationId: String, completion: @escaping (Invitations) -> Void)
然后我返回fetchInfoAboutInvitations函数,以尝试:
info.getInfoAboutInvitationAfterSocket(invitationId: text)
{ (invitation) in
print("some inv: \(invitation)") // -- here i got data
self.arrList.append(invitation)
DispatchQueue.main.async {
self.tableViewController.reloadData()
}
关于这一点:
var arrList = [Invitations]() {
didSet {
print("arrList: \(arrList)")
}
}
我有一个带有数据的打印输出,但tableview是空的。如果我重新运行我的应用程序,我的tableview将不会为空。另一个功能可以正常工作:
func getCurrentEvent() {
currentInvitations.getCurrentInvitations { (value) in
print("currentInv value get CURINVI: \(value)")
self.arrList.append(value)
DispatchQueue.main.async {
self.tableViewController.reloadData()
}
}
}
我的视图加载:
override func viewDidLoad() {
super.viewDidLoad()
tableViewController.delegate = self
tableViewController.dataSource = self
getCurrentEvent()
}
我想我不能在var arrList=investments中追加数据的原因是因为我在viewDidLoad中没有func fetchInfoAboutInvitations(text:String),但我不能将其添加进去,因为它是一个闭包
更新:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableViewController.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? EventControllerCell {
cell.nameLabel.text = arrList[indexPath.row].id
return cell
}
return UITableViewCell()
}
更新#2,添加一些细节:
这里我从websocket(服务器)获取事件
然后我触发:
func fetchInfoAboutInvitations(text: String) {
let info = InfoAboutInvitation()
info.getInfoAboutInvitationAfterSocket(invitationId: text) { (invitation) in
print("some inv: \(invitation)")
self.arrList.append(invitation)
DispatchQueue.main.async {
self.tableViewController.reloadData()
}
}
}
完成后的请求:
func getInfoAboutInvitationAfterSocket (invitationId: String, completion: @escaping (Invitations) -> Void) {
guard var urlComp = URLComponents(string: "") else {return}
urlComp.path = ""
guard let url = urlComp.url else { return }
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("a")
request.addValue("", forHTTPHeaderField: "")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
print("URLSession error")
print(error!)
}
guard let data = data else {
print("data is empty")
return
}
do {
let getData = try! JSONDecoder().decode(Invite.self, from: data)
let invitionData = Invitations(id: getData.id, initiator: getData.initiator, receiver: getData.receiver, group: getData.group, created: nil, status: getData.status, replieadAt: getData.replieadAt)
print("info about invitationData (infAbout): \(invitionData)")
completion(invitionData)
} catch let err {
print(err)
}
}
task.resume()
}
如果问题还不清楚,有人能告诉我如何用另一种方式来实现这一点吗?我不知道如何将它用于委托\协议(以及如何实现)
我在另一台电脑上运行了该应用程序,现在我在tableView中有空值,当关闭工作时:
func fetchInfoAboutInvitations(text: String) {
info.getInfoAboutInvitationAfterSocket(invitationId: text) { (invitation) in
print("some inv: \(invitation)")
self.arrList.append(invitation)
DispatchQueue.main.async {
self.tableViewController.reloadData() // - here I got nil
}
}
}
如果tableViewController是UITableViewController的实例,则需要在UITableViewController的
tableView
属性上调用reloadData()
,如self.tableViewController.tableView.reloadData()
,如果tableViewController是UITableViewController的实例,您需要在UITableViewController的tableView
属性上调用reloadData()
,如self.tableViewController.tableView.reloadData()
否,其UITableView是否可以共享您的func tableView(UITableView,numberOfRowsInSection:Int)->Int
,func numberOfSections(在:UITableView)->Int
和func numberOfSections(在:UITableView)->Int
实现中?我在主帖子的P.S.中添加了“更新”。当我第一次运行应用程序时(当工作方法viewDidLoad时),我的表视图不是空的(如果我有一些事件)。问题在于更新tableView中的新事件否,其UITableView能否请您共享func tableView(UITableView,numberOfRowsInSection:Int)->Int
,func numberOfSections(in:UITableView)->Int
实现?我添加了“更新”在主帖子中,当我第一次运行应用程序时(当工作方法viewDidLoad时),我的tableView不是空的(如果我有一些事件)。问题在于tableView中的更新新事件
func fetchInfoAboutInvitations(text: String) {
info.getInfoAboutInvitationAfterSocket(invitationId: text) { (invitation) in
print("some inv: \(invitation)")
self.arrList.append(invitation)
DispatchQueue.main.async {
self.tableViewController.reloadData() // - here I got nil
}
}
}