Template for updating UItableView and its data source after filtering with NSPredicate

It seems like it should be straight forward. Technically I can make this work, but if I want to perform certain UI animations, (using deleteRowsAtIndexPaths: withRowAnimation :) things get hairy. It seems that the data source and UI are struggling with who should delete first. Then I have to artificially store the data to update the UI.

My current setup:

In my model:

  • Create a multidimensional NSArray to hold my data. (each sub array represents a section).
  • Place an NSDictionary in each section of the array to represent the data for a separate cell.

In my view controller:

  • Create a multidimensional NSArray to hold cell controllers (mimicking a data structure).
  • Assign each model vocabulary ivar in the appropriate Controller cell.
  • Connect data to cell

This displays my UITableView on the screen with the cells rendered correctly. Now, if I want to filter data in a table, I do the following:

In my model:

  • Create an instance of NSPredicate.
  • Filter each subframe multidimensional array.
  • Set a flag in each dictionary that is within the results of the filtered array.

In my Controller View:

  • I am creating a new multidimensional NSArray. This array will contain the "filtered Data" cell controllers
  • Loop through the current Multidimensional array of controllers. Add it to a new "filtered array if the model flag is set."
  • Replace the unfiltered controller array with a new one.
  • Refresh the table.

It also works really well. For now, I don't mind updating the UI. If I want to delete rows with animation, I have to pass these indices to the table to be dropped.

To accomplish this, add a few more steps to the view controller:

In the View controller:

  • I am creating a new multidimensional NSArray. This array will contain the "filtered Data" cell controllers
  • Loop through the current Multidimensional array of controllers. Add it to a new "filtered array if the model flag is set."
  • Create a new NSArray to hold the indexes to be dropped.
  • Loop through the current Multidimensional array of controllers. Add your cell index to the "toBeDeleted" array if its model flag is NOT set.
  • Replace the unfiltered controller array with a new one.
  • execute deleteRowsAtIndexPaths :. withRowAnimationn

This method doesn't work yet. I'm having problems deleting rows with edges (blank sections and all visible cells are removed).

This made me think maybe my methodology is flawed. Has anyone implemented this in an app? How did you decide this? Do your line animations work?

+1


a source to share


1 answer


Not worse, but have you tried NSPredicate on the device itself?

I found that NSPredicate worked on the Simulator, but the same as NSXMLDocument was not available on the device itself (2.2.1).


update: Given that NSPredicate is available, all I can add is that I had unpredictable results with deleteRowsAtIndexPaths: withRowAnimation - I had to settle for the reloadData call and the non-animated jump.



Fortunately, since I have a very "subtractive" filter in my application, there is usually a lot of movement there, so it is obvious to the user that something has happened.

In the case where the visible changes are potentially not obvious, I suspect that from a usability point of view it is quite easy to use an animated UIImageView moving from north to south through the UITableView area. (unfortunately the problem is in his own right).

This will give the added bonus of confirming the filter application to the user, even if no visible change has occurred.

0


a source







All Articles