2012-07-04 25 views
0

我有這樣的代碼在我的tableView實施:暫時移除未編輯單元格的UITableView

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     return NO; 
    } 
    return YES; 
} 

據我想要做什麼,但我想一步到位,讓「一節0」完全消失時,編輯按鈕被按下(如果你進入iOS的「鍵盤」菜單並選擇右上角的編輯,頂部兩部分在動畫中消失,可以看到這種效果)。我曾嘗試暫時刪除的第一部分,但是當[tableView reloadData];叫我的應用程序崩潰:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tvController.editing == YES) { 
     return 1; 
    }else if (tvController.editing == NO) { 
     return 2; 
    } 
    return 0; 
} 

另外,我不認爲我將結束與動畫,如果我得到的代碼工作,我覺得我的做法是錯的。謝謝你們的幫助!

+0

你是否研究過'UITableView'中'deleteSections ::'和'insertSections ::'的使用? –

+0

因此,如果我使用deleteSections刪除第0部分,那麼第1部分將變成第0部分? – prince

+0

是第1部分將成爲第0部分。但只是一個觀察。縱觀鍵盤在iPad上的工作方式,我幾乎可以說他們是完全不同的兩種觀點,他們正在爲從一種到另一種的過渡創造動力。請注意左上方的導航按鈕消失。說過你可以做到這一點,因爲菲利普建議通過deleteSections和一些動畫。 –

回答

1

你的問題

你的一個部分都是比前一個更長的時間。

由於您通過在numberOfSectionsInTableView:中報告1個較少部分來隱藏部分0,所以在編輯模式下,每個委託方法必須調整部分編號。其中之一併沒有這樣做。

// every delegate method with a section or indexPath must adjust it when editing 

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tvController.editing) section++; 
    return [[customers objectAtIndex:section] count]; 
} 

- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath 
{ 
    int section = indexPath.section; 
    if (tvController.editing) section++; 

    id customer = [[customers objectAtIndex:section] indexPath.row]; 

    // etc 
} 

我的做法

的UITableView與動畫指定部分reloadSections:withRowAnimation:重載。從setEding:animated:委託方法調用它。

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

    UITableViewRowAnimation animation = animated ? UITableViewRowAnimationFade : UITableViewRowAnimationNone; 
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animation]; 

    [self.tableView reloadSectionIndexTitles]; 

    self.navigationItem.hidesBackButton = editing; 
} 

您的委託還需要指示隱藏部分沒有行或標題。

- (NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (self.editing && section == 0) { 
     return 0; 
    } 

    return [[customers objectAtIndex:section] count]; 
} 

- (NSString*) tableView:(UITableView*) tableView titleForHeaderInSection:(NSInteger) section 
{ 
    if (self.editing && section == 0) { 
     return nil; 
    } 

    [[customers objectAtIndex:section] title]; 
} 
+0

嘿,謝謝你的回答,但是你能否在這裏解釋問號:UITableViewRowAnimation animation = animated? UITableViewRowAnimationFade:UITableViewRowAnimationNone;還有,爲什麼當我使用UITableViewRowAnimationFade時它不會褪色? – prince

+0

這是三元運算符,http://stackoverflow.com/questions/10592547/what-does-this-do –

+0

謝謝!這是有道理的,但當動畫被設置爲YES時,我仍然不會獲得動畫。 – prince

相關問題