![](/img/trans.png)
[英]Swift - “attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update”
[英]swift update row error - attempting to add row… but there are 0 rows after update
我正在嘗試使用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
}
您沒有實現numberOfRowsInSection
。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageArray.count
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.