Why does setEditing: animated: and insertRowsAtIndexPaths: lead to this weird editing style animation?

I have a UITableView on which I switch setEditing:animated: to allow the user to insert and delete rows. When editing is turned on, I want to add a new row, insert new item to the table, and then I want the edit controls to animate as usual. I do not want the new line insert new item animate by itself using something like fade. I want it to just appear, and then paste, like any existing source rows of table data.

Here what happens as a result of my current code (click to enlarge):

5ZzjR.gif

The top line does what I want - it just jumps and the delete icon disappears. When it disappears, the delete icon disappears and the line expands again.

The second row is my row containing no data, which I myself add to the table. When it appears, it does not come to life at all. The icon and the insert line appear immediately and are not inserted. When it disappears, the line expands perfectly, but the plus sign slide with it. Animation occurs for the entire line, not for the plus sign, and then the line is separate, like the first line.

Here is a quick code of my code, but I think that providing a link to a class file might be better.

When the edit button on my toolbar is clicked, I call the UIViewController method setEditing:animated: In this method, I do the following ...

 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; // keep the table in the same editing mode [_table setEditing:editing animated:animated]; if (editing) { [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } else { [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } } 

Here the insertion animation takes place. I tried wrapping it all in [_table beginUpdate] and endUpdate, and also just insert a line. It seems that the pure animation I was aiming for is not being created.

Any ideas what I might be missing? The entire code file is here:

https://github.com/ryancole/pound-client/blob/master/pound-client/controllers/ChannelListViewController.m#L106-L127

thanks

+7
source share
2 answers

A super call makes a rolling animation for editing, so if you insert something after it, it will not participate in it. What you want to do is insert a line before this call and delete the line after. You will also need to track rows with different booleans.

 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { _amEditing = editing; if (editing) { [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } [super setEditing:editing animated:animated]; // keep the table in the same editing mode [self.view setEditing:editing animated:animated]; if (!editing) { [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _amEditing ? _channels.count + 1 : _channels.count; } 

UPDATE:

The second to last line icon still has a weird animation. To get around this, you can add a delay to delete.

  if (!editing) { [self performSelector:@selector(deleteLastRow) withObject:nil afterDelay:0.25]; } -(void) deleteLastRow { [self.view deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_objects.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } 
+2
source

As commented out, you should call [super setEditing:editing animated:animated] after you insert the line

  - (void)setEditing:(BOOL)editing animated:(BOOL)animated { // remove this [super setEditing:editing animated:animated]; // keep the table in the same editing mode [_table setEditing:editing animated:animated]; if (editing) { [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } else { [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_channels.count inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } [super setEditing:editing animated:animated]; // add it here } 
+1
source

All Articles