我正在尝试打印在表中声明为空全局变量的messageReceived
数组。我正在尝试打印的数据是使用Web套接字接收的。我正在ViewController.swift
函数中分配数据,我知道数据正在进入程序,因为我在标签中打印,但是我正在尝试在表中打印它的那一刻是简单的不工作。所有这些都在import UIKit
import Starscream
var messagetext: String = ""
var tabletext: String = ""
var chat = [String] ()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
var socket = WebSocket(url: URL(string: "ws://localhost:1337/")!, protocols: ["chat"])
@IBOutlet weak var chatMessage: UILabel!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var tableView: UITableView!
@IBAction func buttonClick(_ sender: Any) {
messagetext = textField.text!
sendMessage(messagetext)
}
override func viewDidLoad() {
super.viewDidLoad()
self.textField.delegate = self
socket.delegate = self
socket.connect()
navigationItem.hidesBackButton = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldDidEndEditing(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return (true)
}
deinit{
socket.disconnect(forceTimeout: 0)
socket.delegate = nil
}
}
// MARK: - FilePrivate
fileprivate extension ViewController {
func sendMessage(_ messager: String) {
socket.write(string: messager)
}
func messageReceived(_ message: String) {
chatMessage.text = message
chat.append(message)
}
}
// MARK: - WebSocketDelegate
extension ViewController : WebSocketDelegate {
public func websocketDidConnect(_ socket: Starscream.WebSocket) {
}
public func websocketDidDisconnect(_ socket: Starscream.WebSocket, error: NSError?) {
performSegue(withIdentifier: "websocketDisconnected", sender: self)
}
public func websocketDidReceiveMessage(_ socket: Starscream.WebSocket, text: String) {
// 1
guard let data = text.data(using: .utf16),
let jsonData = try? JSONSerialization.jsonObject(with: data),
let jsonDict = jsonData as? [String: Any],
let messageType = jsonDict["type"] as? String else {
return
}
// 2
if messageType == "message",
let messageData = jsonDict["data"] as? [String: Any],
let messageText = messageData["text"] as? String {
messageReceived(messageText)
}
}
public func websocketDidReceiveData(_ socket: Starscream.WebSocket, data: Data) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(chat.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = chat[indexPath.row] as! String
return(cell)
}
}
:
CreateAccountForm
答案 0 :(得分:1)
假设您确定视图控制器会收到数据,问题是:在接收任何数据之前,tableview数据源方法称为,这意味着{{1}数据源数组仍为空,因此无法显示数据。
您的案例的解决方案是确保在接收数据(更新chat
数据源数组的值)之后重新加载tableview ,这意味着在您添加消息后的情况下通过调用reloadData() UITableView实例方法在chat
方法中chat
:
messageReceived
答案 1 :(得分:0)
在收到邮件的处理程序中,发出tableview.reloadData()
干杯!
答案 2 :(得分:0)
您需要告诉tableview有新数据。您还需要考虑到网络操作可能发生在后台队列上,并且UI更新必须位于主队列中:
func messageReceived(_ message: String) {
DispatchQueue.main.async {
let newRow = IndexPath(row: chat.count, section:0)
chatMessage.text = message
chat.append(message)
tableView.insertRows(at:[newRow],with: .automatic)
}
}