如何在空数组中打印数据

How to print data in empty array

我正在尝试打印在 table 中声明为空全局变量的 chat 数组。我尝试打印的数据是使用网络套接字接收的。我在 messageReceived 函数中分配数据,并且我知道数据正在进入程序,因为我在标签中打印,但是当我试图在 table 很简单,不起作用。所有这些都在 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)
}



}

在您的消息接收处理程序中,发出 tableview.reloadData()

干杯!

假设您确定视图控制器要接收数据,问题是:tableview 数据源方法在接收任何数据之前被调用,这表示chat数据源数组仍然为空,因此没有数据显示。

针对您的情况的解决方案是确保在接收数据后重新加载表视图(更新chat数据源数组的值),这意味着在您的通过调用 reloadData() UITableView 实例方法将消息附加到 messageReceived 方法中的 chat 后的情况:

func messageReceived(_ message: String) {

    chatMessage.text = message
    chat.append(message)

    // here we go:
    tableView.reloadData()
}

你需要告诉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)
    }
}