iphone - 设置编辑 :animated: is missing animation?

标签 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]; 
} 

} 

最佳答案

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

@selector(setEditing:animated:)

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

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

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

然后在你的按钮上设置 Action :

@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"];
    } 
}

关于iphone - 设置编辑 :animated: is missing animation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8033903/

相关文章:

iphone - 下有tableView的透明tabbar

iphone - iOS 5 实例变量

iphone - 如何删除以前版本的 Xcode

objective-c - 如何使用 nib 文件加载 UIView?

ios - Objective-c self 与内存

iphone - 这段代码可以设置 UITableViewCell 背景颜色吗?

ios - RestKit 中发送两个请求然后仅在两个请求返回后才执行操作的最佳方法是什么?

ios - 出现键盘时向上移动 View

iPhone 和 iPad 应用程序具有不同的 UI 但名称相同

ios - 使用 JSQMessagesViewController 时,UI 不会出现