SWIFT更新行错误 - 尝试添加行..但是更新后有0行



我正在尝试取一个UITableView,并在数组中添加一个带有消息的NIB文件。为了进行测试,我将String进行了硬编码,但由于某种原因,当控制器运行时,请获得以下代码:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 0, but there are only 0 rows in section 0 after the update'
*** 

我还试图将新单元格插入另一行(1和0),因为我不确定这是否是导致错误的原因,但这也不起作用。

这是我正在使用的代码:

import Foundation
import UIKit
import SendBirdSDK
class TestChat: UIViewController {
    @IBOutlet weak var myTableView: UITableView!
    var messageArray = [String]()
    var messages: [SBDBaseMessage] = []
    override func viewDidLoad() {
        //put code below
        super.viewDidLoad()
        //load the channel with the two people
        SBDGroupChannel.createChannel(withUserIds: ["22077272", "22077273"], isDistinct: true, completionHandler: { (channel, error) in
            if(error != nil) {
                print("creation error!!!!")
            } else {
                //below load the sent messages
                let previousMessageQuery = channel?.createPreviousMessageListQuery()
                //since below has a hard coded value of 1 for the amount of messages being loaded, we add that to the array
                self.messageArray.append("Phlare")
                previousMessageQuery?.loadPreviousMessages(withLimit: 1, reverse: true, completionHandler: { (messages2, error) in
                    if error != nil {
                        NSLog("Error: %@", error!)
                        return
                    } else {
                        //print(messages)
                        self.messages = messages2!
                        let msg = self.messages[0]
                        if msg is SBDUserMessage {
                            let userMessage = msg as! SBDUserMessage
                            let sender = userMessage.sender
                            //below is number of current user
                            if sender?.userId == "2077103974" {    //SBDMain.getCurrentUser()?.userId {
                                print(sender!.userId)
                                //populate the table view with a sent message, not received
                                let nib = UINib(nibName: "OutgoingUserMessage", bundle: nil)
                                let view = nib.instantiate(withOwner: self, options: nil)
                                self.myTableView.register(nib, forCellReuseIdentifier: "cell")
                                self.myTableView.beginUpdates()
                                self.myTableView.insertRows(at: [IndexPath(row: 1, section: 0)], with: .automatic)
                                self.myTableView.endUpdates()
                                print("user identified")
                            } else {
                                //populate below with the received xib
                                print("received message")
                            }
                        }
                    }
                    // ...
                })
            }
        })// ends channel creation
    }//end of view did load   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! OutgoingUserMessage
        let msg = messageArray[indexPath.row]
        //below 
        //is
        //a
        //hardcoded
        //value
        cell.messageLabel.text = "Phlare"
        return cell
    }
}

我已删除了我的tableview的功能,但是可以在需要时添加它们。当前,该函数填充的行数量只是1的硬编码值1。下面是我的numberOfRowsInSection代码:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("test coiunt")
    print(messageArray.count)
    print(messageArray)
    //return messageArray.count
    return 1
}

您没有实现numberOfRowsInSection

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

最新更新