4.8. Deleting Cells and Sections from Table Views
Problem
You want to delete sections and/or cells from table views using animations.
Solution
In order to delete sections from a table view, follow these steps:
First delete the section(s) in your data source, whether you are using a data model like Core Data or a dictionary/array.
Invoke the
deleteSections:withRowAnimation:instance method ofUITableViewon your table view. The first parameter that you need to pass to this method is of typeNSIndexSet, and this object can be instantiated using theindexSetWithIndex:class method ofNSIndexSetclass, where the given index is an unsigned integer. Using this approach, you will be able to delete only one section at a time. If you intend to delete more than one section at a time, use theindexSetWithIndexesInRange:class method ofNSIndexSetto create the index set using a range, and pass that index set to the aforementioned instance method ofUITableView.
If you want to delete cells from your table view, follow these steps:
First, delete the cell(s) from your data source. Again, it doesn’t matter if you are using Core Data, a simple dictionary, array, or anything else. The important thing is to delete the objects that represent the table view cells from your data source.
Now, in order to delete the cells that correspond to your data objects, invoke the
deleteRowsAtIndexPaths:withRowAnimation:instance method of your table view. The first parameter that you have to pass to this method is an array ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access