开发者

animation issues when editing a uitableview

开发者 https://www.devze.com 2023-03-31 07:01 出处:网络
This is really simple, though still driving my nuts. I have a uitableview where I am trying to animate transition in and out of editing mode. This is what I took from an example that I have seen. It d

This is really simple, though still driving my nuts. I have a uitableview where I am trying to animate transition in and out of editing mode. This is what I took from an example that I have seen. It does do the job, but without the animation.

Any thoughts?

- (IBAction) EditTable:(id)sender
{

if(self.editing)
{
    [super setEditing:NO animated:YES]; 
    [tblSimpleTable setEditing:NO animated:YES];
    [tblSimpleTable reloadData];
    [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
    [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
    [super setEditing:YES animated:YES]; 
    [tblSimpleTable setEditing:YES animated:YES];
    [tblSimpleTable reloadData];
    [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
    [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}

}

PS: I am also not sure why I need this line: [super setEditing:NO animated:YES]; but things just dont seem to work at all without it. I just saw a few examples online that dont d开发者_如何学Co that.

Thanks!


Maybe you should not reloadData when set editing property.

BTW, What's your "super" class? Normally you don't have to invoke [super setEditing:YES animated:YES];


Is it only the button that isn't animating properly? Either way you should probably be using super.editButtonItem instead of your own; it's animated and just setting the text and style like that (I believe) isn't. As far as calling the super, are you overriding one of the editing methods and not calling the super method from within there? And xuzhes's answer about the reloadData is, I believe, correct as well.


Try this:

@Implementation YourViewController // This can (should) be a subclass of UITableViewController to make your life easier

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem; // Automatically calls setEditing:animated: and changes itself to "Edit"/"Done" between presses
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing == YES) {
        // Do stuff here
    } else {
        // Do stuff here
    }

    // Reload all sections of the table view
    // THIS IS THE PART YOU'RE INTERESTED IN
    NSRange range = NSMakeRange(0,[self.tableView numberOfSections]);
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

Check out the documentation for comments on the methods from Apple :)

0

精彩评论

暂无评论...
验证码 换一张
取 消