2011-11-07 53 views
1

我在編程iOS時遇到了一個問題:當我嘗試爲我的tableView製作自定義編輯按鈕時,我無法將其設置爲動畫。下面是我的初始化實現代碼如下:setEditing:animated:缺少動畫?

- (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而不是我的Add按鈕,編輯模式是動畫。這是我的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]; 
} 

} 

回答

2

我不認爲你是分配選擇能夠發送兩個參數:

@selector(setEditing:animated:) 

一個按鈕動作通常只發送要麼沒有參數,或者本身作爲一個參數(通常爲(id)sender) 。所以你在做什麼可能被解釋爲發送NO作爲第二個參數(所以animated = NO)。

您應該添加一個新的方法,像這樣:

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

,並設置你的按鈕的操作:

@selector(toggleEditing) 

編輯

OK,這裏是一些更詳細的代碼爲你。

對於初學者來說,你最初的代碼示例應在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"]; 
    } 
} 
+0

對不起,我已經試過了,但還是沒有用=( – Yangrui

+0

嘗試取出reloadRows ..電話,它們是什麼,因爲他們會立即更換電池,因此可以發生在你的動畫完成 – jrturton

+0

是的,我已經刪除了我行應修改我的[超級setEditing:NO動畫:是];?。??我只是想在我的導航欄上製作一個自定義編輯按鈕或者我可以在哪裏找到一些易於理解的代碼? – Yangrui