简体   繁体   中英

Unable to insert new row to UITableView with dynamic number of Sections And Rows

I have a tableView with dynamic number of sections and each section has dynamic number of rows.

My datasource array looks like this

var detailsList = [[Any]]()

However I do know the type which would be added to the list and they would be added in a particular order. Lets consider these types would be A, B, C.

Depending upon the availability of data from the API detailsList would be populated. Hence the array would look something like this:

[[A, A, A, A], [B, B, B], [C, C, C]]

In this example, the tableView has 3 sections and numberOfRows are dependent on the subarray count.

Here is how the dataSource looks like

    extension ViewController: UITableViewDataSource {
            func numberOfSections(in tableView: UITableView) -> Int {
                return detailsList.count
            }

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

            func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
               if let _ = detailsList[indexPath.section] as? [A] {
                  return cellA
               } else if let _ = detailsList[indexPath.section] as? [B] {
                 return cellB
               } else if let _ = detailsList[indexPath.section] as? [C] {
                 return cellC
                } else if let _ = detailsList[indexPath.section] as? [D] {
                 return cellD
                }
 }

The problem which I am facing is when I want to insert a section.

Lets say DataSource before adding a array looks like this

detailsList = [[A, A, A, A], [B, B, B], [C, C, C]]

After adding a new section the dataSource looks like this

detailsList = [[A, A, A, A], [B, B, B], [C, C, C], [D, D, D, D]]

I am unable to say

tableView.insertRows(at: [indexPath], with: .none)

App crashes with following exception

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 (4) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

But if i say

tableView.reloadData() it works as expected.

is there another way to insert new section?

You are adding a section, so you need to use

let indexSet = IndexSet( integer:4)
tableView.insertSections( indexSet, with:.fade)

As the error message tells you, you are trying to insert more rows, but what your array is telling you is that you need more sections. You should therefor use:

tableView.insertSections(sections: IndexSet, with: UITableViewRowAnimation)

The problem is that you should call insertRows method inside 'beginUpdates' and 'endUpdates' method calls like so:

//update the array first
detailsList = [[A, A, A, A], [B, B, B], [C, C, C], [D, D, D, D]]

//call insert method inside begin and end updates
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .none)
tableView.endUpdates()

UPD: There is a special method insertSections when adding a section instead of a row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM