Updating TableView table after deletion

I have a table populated with data from an array of objects. Objects have a price, name, etc. Etc.

When the cell is deleted, the data source (Array) is updated, and the row comes off the screen using the UITableViewRowAnimationFade.

When an element is deleted, some properties, such as price, of objects within the array can change, so I need to update all the cells on the screen, as their data can be changed.

I looked through the docs and found reloadRowsAtIndexPaths: withRowAnimation, which I can combine with indexPathsforVisibleRows to reload rows on the screen, but doing it in tableView: commitEditingStyle: forRowAtIndexPath looks very unpleasant as it tries to perform a delete animation and also reload ...

Is there a way to wait for the delete animation to complete before completing the task?

Here is the code from my ViewController

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // update the datasource [self.dataController deleteItemAtIndex:indexPath.row]; // update the table [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; // reload the table to show any changes to datasource from above deletion [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone]; } } 

ASD

+4
source share
1 answer

Edit:

Ok, give it a try. :) This should definitely work, but it takes a lot of effort ...

You will need to accurately track changes in the data source (add, delete, update) and call the appropriate methods in TableVC.

The data source source will need to be provided with the following delegate methods:

 - (void)dataControllerWillUpdateData; - (void)dataControllerDidRemoveObjectAtIndexPath:(NSIndexPath *)indexPath; - (void)dataControllerDidAddObjectAtIndexPath:(NSIndexPath *)indexPath; - (void)dataControllerDidUpdateObjectAtIndexPath:(NSIndexPath *)indexPath; - (void)dataControllerDidUpdateData; 

Then you change the implementation of tableVC as follows:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // update the datasource [self.dataController deleteItemAtIndex:indexPath.row]; } } - (void)dataControllerWillUpdateData { [tableView beginUpdates]; } - (void)dataControllerDidRemoveObjectAtIndexPath:(NSIndexPath *)indexPath { [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } - (void)dataControllerDidAddObjectAtIndexPath:(NSIndexPath *)indexPath { [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } - (void)dataControllerDidUpdateObjectAtIndexPath:(NSIndexPath *)indexPath { [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; } - (void)dataControllerDidUpdateData { [tableView endUpdates]; } 

So, if the user deletes the cell, your data source must determine what other objects are affected, create a change list (be careful to calculate the correct index columns), call dataControllerWillUpdateData , call the corresponding methods above for each changed object, and finally call dataControllerDidUpdateData .

Of course, you can also consider using CoreData in your project. This may require some work to configure everything, but as a result you will get all of the above and much more β€œfor free”. Personally, I tend to use it for almost every project that includes dynamic tables. It has so many advantages that it is most worth the effort.

+1
source

Source: https://habr.com/ru/post/1442478/


All Articles