Filtering UITableViews: Deleting Multiple UITableViewCells with Animation
If you have done any iPhone development, you are already intimately familiar with table views. They are an integral part of UIKit. They are highly flexible and customizable.
However, making table views work smoothly with anything but a small, simple, static data set can be trying, to say the least.
Many Cocoa developers have come up with solutions to cope with scrolling performance issues, abstracting cell content, creating preference views, adding network awareness, etc…
Ideally much of this functionality should have been included in the Apple frameworks. Alas, this is not the case. We must use third party solutions to fill the gaps or “roll our own” solutions. Sometimes we need to combine the work of several developers into one class.
Recently, I had to create a network aware table view that also supported filtering with animation. Already having setup a UITableViewController for receiving network data, all that was needed was to filter my data and animate the change. Simple.
Well, not really. If you want to perform that nifty animation that Apple uses when viewing missed calls in the phone app, you will most likely need to make some changes to your current design.
To analyze the problem, lets look at the major components: data model, presentation model, view. We will stay away from code, and look more at the design to accomplish this.
For my typical table views, Matt Gallagher’s solution works fantastic.

The diagram is a bit simplistic, but illustrative (In reality, we have nested arrays representing sections within the table). There are two arrays, one to hold our model data and one to hold our presentation data. Also note, the structure of the data model does NOT necessarily need to reflect that of the table, but it seems logical to organize this particular set of data in this manner.
We have created a “Cell Controller” to manage individual UITableViewCell logic. The UITableViewController now only has the responsibility of creating and destroying these Cell Controllers. This moves the UITableViewController one step closer to adhering to SRP.
One undesirable aspect of this methodology is the requirement to instantiate every Cell Controller and link it to its model data upfront. In return you receive better flexibility and encapsulation without a noticeable performance loss (at least with the moderate sized data sets I have so far worked with).
So, lets look at our basic algorithm for creating our Cell Controllers:
- Loop through each dictionary in the model data array.
- Create a cell controller.
- Add a pointer to the dictionary in the cell controller.
- Add the cell controller to the array of cell controllers.
This approach works well for static tables and it is easily adapted for network aware tables. For static tables, you set up your relationships once, and your done. For a networked table, you must add a new object to handle network activity. Then they can communicate through any combination of notifications, KVO, and delegate methods.

This is pretty basic, but it is nice to see the relationships laid out.
Now we need to modify our approach to handle filtering. We need a way to represent the full data set and any subsets. Two methods of accomplishing this task are:
- Create separate arrays for each set. One for the full data set and one for the filtered set.
- Use a flag to mark filtered results.
Flagging is preferable for a two reasons. In reality, the data didn’t change, we just want to update the presentation. In this case we don’t want to actually modify our data. Additionally, it is more difficult to track several arrays and which one is the “real” data.
As we stated earlier, the presentation model needs changed to display the filtered results. In our previous design, the view controller “looked” at the model and created the cell controllers based on the structure of the data. Not much has changed, but now we just need an additional check. So our algorithm could be:
- Loop through each dictionary in the data model.
- If a dictionary has the flag set,
- Create a cell controller,
- Add a pointer to the dictionary in the cell controller,
- Add the cell controller to the array of cell controllers.
- Finally, replace the old (unfiltered) array of cell controllers with the new (filtered) one.
As we connect the Cell Controllers to their corresponding model data, we check our flag. If we did not want to animate our deleted cells collapsing, we would be finished. We need to devise a method tell our table view which cells have been removed (as well as sections).
There are a few ways to accomplish this. In place of the previous algorithm we could do the following:
- In our view controller, loop through each cell controller in the array.
- If a cell controller’s model dictionary does not have the flag set,
- Add the cell controller’s index to an indexPathSet to removed from the array
- Add the cell’s index path to the array of cell paths to be removed from the table.
- Remove the cell controllers from the array of cell controllers.
- Remove the table view cells from the UITableView.
Not too bad, but we have completely changed our algorithm. Instead of creating an entire new array of cell controllers, we now remove the ones . The motivation behind this, is that we need to give the table view a list of cells to delete. If we had used our previous algorithm, it would have been difficult to produce this list.
We have just one more issue, but you will not discover it until you try to update a remove all the cells in a section and you app crashes. UITableViews do not like empty sections after deletions. So make sure you remove any empty sections as you remove UITableViewCells.
Hopefully this helps some of you get a head start on your table views. We mostly went over design and algorithms which should help you produce your own code and cut down on much of the trial and error that goes into this problem. In a future post, I will include some code as well.
Let me know if you have already coded your own solution to this or are using open source code.
Tags: development, iPhone, UITableView
June 24th, 2009 at 2:27 pm
Filtering UITableViews…
You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…
July 1st, 2009 at 10:05 am
[...] UITableViews: Deleting Multiple UITableViewCells with Animation – http://www.theflyingjalapenolives.com/2009/06/filtering-uitableviews-deleting-multiple-uitableviewce... [...]
July 13th, 2009 at 9:31 pm
[...] The Flying Jalapeno Lives » Filtering UITableViews: Deleting Multiple UITableViewCells with Animati… (tags: iphone_tutorial iphone_problem tableview iphone_dev) [...]