最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【基本解决】swift中tableView.endUpdates再次出现NSInternalInconsistencyException Invalid update: invalid number of rows in section 0

Swift crifan 4251浏览 0评论

代码:

    func addGroupTopicItem(groupTopicItem:GroupItem) {
        if !self.existedGroupTopic(groupTopicItem.id) {
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.groupTopicItemList.append(groupTopicItem)
                let insertedRowIdx = self.groupTopicItemList.count 1

                let curRealRowNum = self.tableView.numberOfRowsInSection(0)
                let curRealRowMaxIdx = curRealRowNum – 1
                if insertedRowIdx > curRealRowMaxIdx {
                    self.tableView.reloadData()
                } else {
                    //insert row
                    self.tableView.beginUpdates()
                    let insertedRowIndexPath:NSIndexPath = NSIndexPath(forRow: insertedRowIdx, inSection: 0)
                    self.tableView.insertRowsAtIndexPaths([insertedRowIndexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
                    self.tableView.endUpdates()
                }
               
//                //insert row
//                self.tableView.beginUpdates()
//                let insertedRowIndexPath:NSIndexPath = NSIndexPath(forRow: insertedRowIdx, inSection: 0)
//                self.tableView.insertRowsAtIndexPaths([insertedRowIndexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
//                self.tableView.endUpdates()
            })
        }

    }

‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (50) must be equal to the number of rows contained in that section before the update (50), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’

搜:

swift tableView.endUpdates NSInternalInconsistencyException Invalid update: invalid number of rows in section 0

ios – Invalid update: invalid number of rows in section 0 terminating with uncaught exception of type NSException – Stack Overflow

ios – Invalid update: invalid number of rows in section 0 – Stack Overflow

ios – ‘Invalid update: invalid number of rows in section 0 – Stack Overflow

ios – Invalid update: invalid number of rows in section – Stack Overflow

NSInternalInconsistencyException(Invalid update: invalid number of rows in section 0) – ios

ios – Reload Table view cell with animation (Swift) – StackOverflow

swift insertRowsAtIndexPaths NSInternalInconsistencyException

ios – NSInternalInconsistencyException in insertRowsAtIndexPaths – Stack Overflow

去改为:

                self.tableView.beginUpdates()
                self.groupTopicItemList.append(groupTopicItem)
                let insertedRowIdx = self.groupTopicItemList.count 1
                let insertedRowIndexPath:NSIndexPath = NSIndexPath(forRow: insertedRowIdx, inSection: 0)
                self.tableView.insertRowsAtIndexPaths([insertedRowIndexPath], withRowAnimation: UITableViewRowAnimation.Bottom)
                self.tableView.endUpdates()

结果:

有时候还是会出错:

‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’

ios – How to protect tableview from crashing during insertRowsAtIndexPaths and device rotation – Stack Overflow

最后还是无法解决,最终暂时用了:

self.tableView.reloadData()

。。。

转载请注明:在路上 » 【基本解决】swift中tableView.endUpdates再次出现NSInternalInconsistencyException Invalid update: invalid number of rows in section 0

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.175 seconds, using 22.15MB memory