简体   繁体   中英

Xcode iOS UITableView Insert New Cell Animation

Thanks in advance. I'm dealing with a single UITableView which is divided into different sections. Users can add or delete cells only from specific sections. The information displayed in the UITableView is stored in an array where each object represents a section:

eg: tableItemsArray = ((arrayForSection1), (arrayForSection2), (arrayForSection3))

I have the animation set up to handle the delete as follows:

[tableView deleteRowsAtIndexPaths:@[indexPath]
                      withRowAnimation:UITableViewRowAnimationRight];

I then programmatically remove the item from the array to remove it from the data model. However I am having difficulty in how to insert the cell in an animated form.

Currently I am running the current pseudo-algorithm:

  1. Check item isn't anywhere in the array
  2. Add item to array into a specific section
  3. sort this section of the array alphabetically
  4. reload the data

Update

When a new item is added to the array it is stored in a string to keep record of the new item. Once the array has been resorted I iterate through the array of cells to find its indexPath, then I delete the string (to prevent recursive insertion animations) and perform the animation, however the code doesn't animate its insertion into the table. I wondered whether that maybe the animation code (as below) wasn't working as I was using [tableView reloadData]?

// For Each Section in the table:
        for (NSMutableArray *object in sectionsInTable) {

            // For Each Cell in a section (Cells are stored in an array in index 1)
            for (NSString *label in [object objectAtIndex:1]) {
                if ([label isEqualToString:_insertedObject]) {
                    // New item
                    _insertedObject = Nil;
                    // animate addition
                    [self.labelListTableView beginUpdates];
                    [self.labelListTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[[object objectAtIndex:1] indexOfObject:label] inSection:[sectionsInTable indexOfObject:object]]] withRowAnimation:UITableViewRowAnimationBottom];
                    [self.labelListTableView endUpdates];
                }
            }
        }

Any ideas why the animation isn't working?

Thanks

D

Seems that the [tableView reloadData] was preventing the animation.

// Annimate the new Item
        // For Each Section in the table:
        for (NSMutableArray *object in sectionsInTable) {
            // For Each Cell in a section:
            for (NSString *cell in [object objectAtIndex:1]) {
                if ([cell isEqualToString:_insertedObject]) {
                    // New item
                    _insertedObject = Nil;
                    // animate addition
                    [tableView beginUpdates];
                    NSInteger section = [sectionsInTable indexOfObjectIdenticalTo:object];
                    NSInteger row = [[object objectAtIndex:1] indexOfObjectIdenticalTo:cell];

                    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
                    [tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom ];
                    [tableView endUpdates];
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM