gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:19:23 28 4
gpt4 key购买 nike

我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com