最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘attempt to insert row 16 into section 0, but there are only 0 rows in section 0 after the update’

Swift crifan 1300浏览 0评论

代码:

            //insert to correct position according datetime
            var insertedRowIdx:Int = 0
            var hasInserted:Bool = false
            for (curIdx, curMessage) in msgTVC.messageList.enumerate() {
    //            print("[\(curIdx)] newMessage.timestamp=\(newMessage.timestamp), curMessage.timestamp=\(curMessage.timestamp), newMessage.text=\(newMessage.text)")
                if newMessage.timestamp < curMessage.timestamp {
                    msgTVC.messageList.insert(newMessage, atIndex: curIdx)
                    insertedRowIdx = curIdx
                    hasInserted = true
                    break
                }
            }
           
            if !hasInserted {
                msgTVC.messageList.append(newMessage)
                insertedRowIdx = msgTVC.messageList.count 1
                hasInserted = true
            }
           
            //update timestamp string
            if insertedRowIdx > 0 {
                let prevMessage = msgTVC.messageList[insertedRowIdx – 1]
                newMessage.timestampStr = generateTimestampStr(newMessage.timestamp, prevTimestamp: prevMessage.timestamp)
            } else {
                newMessage.timestampStr = generateTimestampStr(newMessage.timestamp, prevTimestamp: nil)
            }
    //        print("insertedRowIdx=\(insertedRowIdx)")
            let curRealRowNum = msgTVC.messageTableView.numberOfRowsInSection(0)
           
           
            //insert row
            let insertedRowIndexPath:NSIndexPath = NSIndexPath(forRow: insertedRowIdx, inSection: 0)
//            msgTVC.messageTableView.beginUpdates()
            msgTVC.messageTableView.insertRowsAtIndexPaths([insertedRowIndexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
//            msgTVC.messageTableView.endUpdates()
           
            //here consider inserted to index=0, is get history message
            //-> should not scroll upside
            if isCurrentShowingVc(msgTVC) && (insertedRowIdx != 0){
                //only when current vc, then scroll to that row
                msgTVC.messageTableView.scrollToRowAtIndexPath(insertedRowIndexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
            }

中的:

insertRowsAtIndexPaths

又挂了:

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

以及:

enqueued from NSOperationQueue

如图:

搜:

tableview NSInternalInconsistencyException enqueued from NSOperationQueue

insertRowsAtIndexPaths  NSInternalInconsistencyException

改为:

           msgTVC.messageTableView.beginUpdates()
            //insert to correct position according datetime
            var insertedRowIdx:Int = 0
            var hasInserted:Bool = false
            for (curIdx, curMessage) in msgTVC.messageList.enumerate() {
    //            print("[\(curIdx)] newMessage.timestamp=\(newMessage.timestamp), curMessage.timestamp=\(curMessage.timestamp), newMessage.text=\(newMessage.text)")
                if newMessage.timestamp < curMessage.timestamp {
                    msgTVC.messageList.insert(newMessage, atIndex: curIdx)
                    insertedRowIdx = curIdx
                    hasInserted = true
                    break
                }
            }
           
            if !hasInserted {
                msgTVC.messageList.append(newMessage)
                insertedRowIdx = msgTVC.messageList.count 1
                hasInserted = true
            }
           
            //update timestamp string
            if insertedRowIdx > 0 {
                let prevMessage = msgTVC.messageList[insertedRowIdx – 1]
                newMessage.timestampStr = generateTimestampStr(newMessage.timestamp, prevTimestamp: prevMessage.timestamp)
            } else {
                newMessage.timestampStr = generateTimestampStr(newMessage.timestamp, prevTimestamp: nil)
            }
    //        print("insertedRowIdx=\(insertedRowIdx)")
            //insert row
            let insertedRowIndexPath:NSIndexPath = NSIndexPath(forRow: insertedRowIdx, inSection: 0)
            msgTVC.messageTableView.insertRowsAtIndexPaths([insertedRowIndexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
            msgTVC.messageTableView.endUpdates()

还是不行。

搜:

ios numberofrowsinsection not called

ios numberofrowsinsection not correct

ios numberofrowsinsection not same with list count

最后改为:

            //insert to correct position according datetime
            var insertedRowIdx:Int = 0
            var hasInserted:Bool = false
            for (curIdx, curMessage) in msgTVC.messageList.enumerate() {
    //            print("[\(curIdx)] newMessage.timestamp=\(newMessage.timestamp), curMessage.timestamp=\(curMessage.timestamp), newMessage.text=\(newMessage.text)")
                if newMessage.timestamp < curMessage.timestamp {
                    msgTVC.messageList.insert(newMessage, atIndex: curIdx)
                    insertedRowIdx = curIdx
                    hasInserted = true
                    break
                }
            }
           
            if !hasInserted {
                msgTVC.messageList.append(newMessage)
                insertedRowIdx = msgTVC.messageList.count 1
                hasInserted = true
            }
           
            //update timestamp string
            if insertedRowIdx > 0 {
                let prevMessage = msgTVC.messageList[insertedRowIdx – 1]
                newMessage.timestampStr = generateTimestampStr(newMessage.timestamp, prevTimestamp: prevMessage.timestamp)
            } else {
                newMessage.timestampStr = generateTimestampStr(newMessage.timestamp, prevTimestamp: nil)
            }
    //        print("insertedRowIdx=\(insertedRowIdx)")
            let curRealRowNum = msgTVC.messageTableView.numberOfRowsInSection(0)
            let curRealRowMaxIdx = curRealRowNum – 1
            if insertedRowIdx < curRealRowMaxIdx {
                //insert row
                msgTVC.messageTableView.beginUpdates()
                let insertedRowIndexPath:NSIndexPath = NSIndexPath(forRow: insertedRowIdx, inSection: 0)
                msgTVC.messageTableView.insertRowsAtIndexPaths([insertedRowIndexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
                msgTVC.messageTableView.endUpdates()
                //here consider inserted to index=0, is get history message
                //-> should not scroll upside
                if isCurrentShowingVc(msgTVC) && (insertedRowIdx != 0){
                    //only when current vc, then scroll to that row
                    msgTVC.messageTableView.scrollToRowAtIndexPath(insertedRowIndexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
                }
            }

基本确保了,insertRowsAtIndexPaths不会挂了。。。

转载请注明:在路上 » [已解决]Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘attempt to insert row 16 into section 0, but there are only 0 rows in section 0 after the update’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
64 queries in 0.094 seconds, using 18.91MB memory