Note that there are some explanatory texts on larger screens.

plurals
  1. POPattern to update a UItableView and its datasource after filtering with NSPredicate
    text
    copied!<p>This 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 as though the datasource and UI are fighting over who should delete first. Then I have to artificially keep data around for the UI update.</p> <p>My current setup:</p> <p>In my model:</p> <ol> <li>Create a multidimensional NSArray to hold my data. (each sub array represents a section).</li> <li>Place a NSDictionary in each section array to represent the data for an individual cell.</li> </ol> <p>In my View Controller:</p> <ol> <li>Create a multidimensional NSArray to hold my Cell Controllers (mimicking the data structure).</li> <li>Assign each model dictionary to an ivar in the appropriate Cell Controller.</li> <li>Hook up the data to the cell</li> </ol> <p>This gets my UITableView on screen with cells properly displayed. Now if I want to filter the data in the table I do the following:</p> <p>In my model:</p> <ol> <li>Create an instance of NSPredicate.</li> <li>Filter each subarray of the multidimensional array.</li> <li>Set a flag in each dictionary that is within the results of the filtered array.</li> </ol> <p>In my View controller:</p> <ol> <li>I create a new Multidimensional NSArray. This array will hold the cell controllers of the "filtered data"</li> <li>Loop through the current Multidimensional Controller array. Add it to the new "filtered array if it's model's flag is set.</li> <li>Replace the unfiltered Controller array with the new one.</li> <li>Update the table.</li> </ol> <p>This also works very well. As long as I don't mind the UI updating instantaneously. If I want to delete rows with animation, I must pass those indexes to the table to be deleted.</p> <p>To accomplish this, I add a few more steps to the view controller:</p> <p>In my View controller:</p> <ol> <li>I create a new Multidimensional NSArray. This array will hold the cell controllers of the "filtered data"</li> <li>Loop through the current Multidimensional Controller array. Add it to the new "filtered array if it's model's flag is set.</li> <li>Create a new NSArray to hold indexes to be deleted.</li> <li>Loop through the current Multidimensional Controller array. Add its cell's index to the "toBeDeleted" array if its model's flag is NOT set.</li> <li>Replace the unfiltered Controller array with the new one.</li> <li>perform deleteRowsAtIndexPaths:withRowAnimationn.</li> </ol> <p>So far this method doesn't work the greatest. I am having issues deleting the rows with edge cases (empty sections and all visible cells being deleted).</p> <p>This made me think maybe my methodology is flawed. Has anyone implemented this in an app? How did you solve this? Do your row animations work?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload