首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >setEditing:animated:是否缺少动画?

setEditing:animated:是否缺少动画?
EN

Stack Overflow用户
提问于 2011-11-07 15:49:13
回答 1查看 2.8K关注 0票数 1

我在编程iOS时遇到了一个问题:当我试图为我的tableView定制编辑按钮时,我就是不能让它有动画效果。下面是我初始化tableview的过程:

代码语言:javascript
复制
- (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: method。我可以让我的表视图进入编辑模式,插入按钮立即出现在左边。我尝试过[myTableView reloadRowsAtIndexPaths:[myTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];,但它在iOS 4.3上不起作用。

代码语言:javascript
复制
- (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]; 
} 

} 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-07 16:56:45

我不认为你赋值的选择器能够发送两个参数:

代码语言:javascript
复制
@selector(setEditing:animated:)

按钮操作通常不发送参数,或者将其本身作为参数发送(通常为(id)sender)。因此,您所做的可能会被解释为发送NO作为第二个参数(所以是animated = NO)。

您应该添加一个新方法,如下所示:

代码语言:javascript
复制
-(void)toggleEditing
{
    [self setEditing:!self.isEditing animated:YES];
}

并设置按钮上的操作:

代码语言:javascript
复制
@selector(toggleEditing)

编辑

好的,下面是一些更详细的代码。

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

代码语言:javascript
复制
- (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方法应该如下所示:

代码语言:javascript
复制
- (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"];
    } 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8033903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档