刪除cell



如果想實現滑動(輕掃)cell右邊就能顯示一個刪除按鈕,則要實現tableview 的datasource的方法:

(1)- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 


此方法就是給一個空的實現滑動也會出現刪除按鈕!!

可能我們會對按鈕的出現和消失的時刻感興趣,那麼此刻要實現代理的方法:

(2)- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {


}


(3)- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {

}


在刪除按鈕顯示出來之前會調用(2)方法,給我們處理問題的時間。
其實上述方法的調用順序就是 (2)--->(1)----->(3)



在1方法中,我們就是用來刪除cell,一般調用方法

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]withRowAnimation:UITableViewRowAnimationTop];


後面動畫隨意,如果想讓自己的數據源一起改變,則在調用刪除cell之前,記住一定是之前,在源的數組或者字典中刪除對應的記錄。


下面是我的實現可以參考:


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

NSLog(@"%d", indexPath.row);

[self.myArray removeObjectAtIndex:[indexPath row]];

       [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]withRowAnimation:UITableViewRowAnimationTop];

}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章