setEditing:animated:缺少动画?

时间:2011-11-07 07:49:13

标签: iphone objective-c ios uitableview

我在编程iOS时遇到了一个问题:当我尝试为我的tableView创建自定义编辑按钮时,我就无法将其设置为动画。以下是我初始化tableview的方法:

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

    myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; 
    myTableView.delegate = self; 
    myTableView.dataSource = self; 

    myTableView.autoresizesSubviews = YES; 

    .......

    self.view = myTableView; 

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(setEditing:animated:)];     
    self.navigationItem.rightBarButtonItem = addButton; 
    [addButton release]; 
} 

但是,当我使用self.editButtonItem而不是我的addButton时,编辑模式是动画的。这是我的setEditing:animated:方法。我可以让我的tableview进入编辑模式,插入按钮立即出现在左侧。我试过[myTableView reloadRowsAtIndexPaths:[myTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic]; 但它对iOS 4.3没有帮助。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ 

if(self.editing){ 
    [super setEditing:NO animated:YES]; 
    [myTableView reloadRowsAtIndexPaths:[myTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [myTableView reloadData]; 
    self.navigationItem.title = [selectedCellItem valueForKey:@"name"]; 
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(setEditing:animated:)];  
    self.navigationItem.rightBarButtonItem = addButton; 
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain]; 
    [addButton release]; 
} 
else { 
    [super setEditing:YES animated:YES]; 
    [myTableView reloadRowsAtIndexPaths:[myTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    self.navigationItem.title = @"Add to Favourites"; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(setEditing:animated:)];     
    self.navigationItem.rightBarButtonItem = doneButton; 
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; 
    [doneButton release]; 
    [myTableView reloadData]; 
} 

} 

1 个答案:

答案 0 :(得分:2)

我认为您分配的选择器不能发送两个参数:

@selector(setEditing:animated:)

按钮操作通常只发送无参数或自身作为参数(通常为(id)sender)。所以你所做的可能被解释为发送NO作为第二个参数(所以animated = NO)。

你应该像这样添加一个新方法:

-(void)toggleEditing
{
    [self setEditing:!self.isEditing animated:YES];
}

并在按钮上设置操作:

@selector(toggleEditing)

修改

好的,这里有一些更详细的代码。

对于初学者,您的初始代码示例应该在viewDidLoad而不是viewWillAppear。看起来应该是这样的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad];

    .... code where you set up the table view...

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(toggleEditing)];     
    self.navigationItem.rightBarButtonItem = addButton; 
    [addButton release]; 
} 

您的setEditing方法应该是这样的:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (editing)
    {
        // We are changing to edit mode
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(toggleEditing)];     
        self.navigationItem.rightBarButtonItem = doneButton; 
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; 
        [doneButton release];
        self.navigationItem.title = @"Add to Favourites";
    }
    else
    {
        // We are changing out of edit mode
        self.navigationItem.title = [selectedCellItem valueForKey:@"name"]; 
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(toggleEditing)];  
        self.navigationItem.rightBarButtonItem = addButton; 
        [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain]; 
        [addButton release];
        self.navigationItem.title = [selectedCellItem valueForKey:@"name"];
    } 
}