0

I'm adding data in my model and model is assigned to tableview to reload data. But every time reloading is not looking good. so I want just last element that was added in model, should be appended in already exist tableview. Tried so many ways but getting crash when my tableview is empty.

        let lastSectionIndex = self.isGroupChat ? self.objGroupChatList!.count-1 : self.objSingleChatList!.count-1
        var lastRow = 0
        if self.isGroupChat {
            lastRow = (self.objGroupChatList?[lastSectionIndex].count ?? 1)
        } else {
            lastRow = (self.objSingleChatList?[lastSectionIndex].count ?? 1)
        }
        let IndexPathOfLastRow = IndexPath(row: lastRow-1, section: lastSectionIndex)

        self.tableView.beginUpdates()
        self.tableView.insertRows(at: [IndexPathOfLastRow], with: UITableViewRowAnimation.none)
        self.tableView.endUpdates()

This is crashing with error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
Krutika Sonawala
  • 1,065
  • 1
  • 12
  • 30
  • Where do you add the item to the data source array? And – not related – delete the `beginUpdates/endUpdates` lines. They are pointless. – vadian Jul 26 '19 at 09:35
  • You have to append to data to your datasource model first. Then call `insertRows` (`beginUpdates()` and `endUpdates()` are redundant ) – Ricky Mo Jul 26 '19 at 09:36
  • I have added data into the model before inserting the rows – Krutika Sonawala Jul 26 '19 at 09:41
  • Possible duplicate of [Error 'Invalid update: invalid number of rows in section 0' attempting to delete row in table](https://stackoverflow.com/questions/30516970/error-invalid-update-invalid-number-of-rows-in-section-0-attempting-to-delete) – dahiya_boy Jul 26 '19 at 09:41
  • @KrutikaSonawala Before **appending/deleting** your cells, you need to make same changes in your datasource i.e. `self.objGroupChatList` – dahiya_boy Jul 26 '19 at 09:43
  • @dahiya_boy I have already added element in array – Krutika Sonawala Jul 26 '19 at 09:44
  • Add the code where you've added the element to array. – PGDev Jul 26 '19 at 09:44
  • You say it crashes when the table is empty right? That is because you calculate your last row as (count - 1), if count is zero, last row would be -1. Make sure, it is not negative. – AjinkyaSharma Jul 26 '19 at 09:45
  • @AjinkyaSharma Yes it crashes only when table is empty. With this, my last section = 0 and lastRow = 0 – Krutika Sonawala Jul 26 '19 at 09:47
  • @PGDev added the function – Krutika Sonawala Jul 26 '19 at 09:51
  • So if your last row is 0, your IndexPathOfLastRow would be (-1,0). This is where it is crashing. Make sure, you don't create index path wrong – AjinkyaSharma Jul 26 '19 at 09:51
  • Did you assign a new array to your datasource when you put the first element? i.e. If you do `self.objGroupChatList = [[/*your first element */]]`, this error would occur since the datasource reference has changed. Make sure you never assign new object to your datasource and always modify it under the same reference. – Ricky Mo Jul 26 '19 at 09:51
  • @RickyMo yes the reference is same. – Krutika Sonawala Jul 26 '19 at 09:54
  • Try replacing `self.objGroupChatList = [elements]; let dict = self.objGroupChatList!.first!.categorise { $0.dtSendDate }` with `let dict = [elements].first!.categorise { $0.dtSendDate }`. I see no point to store it since you `removeAll` afterwards, plus this is changing the reference. – Ricky Mo Jul 26 '19 at 10:00
  • @RickyMo doesn't make any difference. – Krutika Sonawala Jul 26 '19 at 10:05
  • 1
    Oh I see. The problem is you are creating new section. You should call insertSections – Ricky Mo Jul 26 '19 at 10:09

2 Answers2

1

You should use insertSections for new sections. insertRows only works for existing sections.

Ricky Mo
  • 6,285
  • 1
  • 14
  • 30
  • How would I get to know that I need to add section or row? I have multiple sections and rows. – Krutika Sonawala Jul 26 '19 at 10:14
  • Unfortunatly you have to write your own logic to compare your current data with the new data or keeping track of the progress before update. – Ricky Mo Jul 26 '19 at 10:17
  • 1
    If your data is only growing, you can simply check if the section you intend to insert into exists or not. – Ricky Mo Jul 26 '19 at 10:22
0

You need to do something like,

    let section = 0 //get your section here...
    dataSource[section].append("five")
    let row = dataSource[section].count - 1
    tableView.insertRows(at: [IndexPath(row: row, section: section)], with: .none)

This is just an example of how you can get that working. Fill the gaps as per your code.

PGDev
  • 23,751
  • 6
  • 34
  • 88