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

时间:2017-09-18 14:18:37

标签: ios swift uitableview

我正在尝试使用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的硬编码值。以下是我的numberOfRowsInSection代码:

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

1 个答案:

答案 0 :(得分:0)

您没有实施numberOfRowsInSection

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