作為一名jr,虎撲體育幾乎是我每天都得打開的app蚕甥,賽季期間自不必說哪替,長草期也喜歡看看八卦和交易動態(tài)。
那么菇怀,我們來看看這款app里的這個效果圖:
這是一個非常標(biāo)準(zhǔn)的ios UITableView的編輯模式凭舶,有添加、刪除敏释、移動三種編輯效果库快。
當(dāng)你新建UITableView時,是不會有編輯模式的钥顽,要實現(xiàn)它的編輯模式义屏,需要按下面的步驟:
1、開啟tableView的編輯效果
設(shè)置UITableView的editing屬性為YES,或調(diào)用setEditing: animated方法闽铐,后者有動畫
2蝶怔、實現(xiàn) - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 方法
這個方法是確定是否哪些行是有編輯效果。如果在1的步驟里設(shè)置了editing屬性為YES兄墅,那么即使2這個方法不實現(xiàn)踢星,默認(rèn)是全部都有編輯效果的。
3隙咸、實現(xiàn) - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 方法
這個方法是決定每一行的編輯類型是什么沐悦,即:UITableViewCellEditingStyle,是一個枚舉
分別是代表沒有編輯類型五督、刪除類型(cell左邊是綠色圓藏否,中間紅色線)、添加類型(cell左邊是藍(lán)色圓充包,中間是加號)副签。值得注意的是移動的編輯類型是不在這個方法里表現(xiàn)的,要想有移動效果基矮,得看下面的方法
4淆储、實現(xiàn) ?- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 方法
這個方法是確定哪些行是有移動模式的。如果不實現(xiàn)這個方法家浇,如果你代碼里有moveRowAtIndexPath這個方法本砰,那么每一行也是有移動模式的(這個方法里面可以不寫任何代碼,僅僅只要有這個方法在蓝谨,就會讓每一行都有移動模式)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
5灌具、實現(xiàn) - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 方法
前面4個步驟已經(jīng)讓我們每一行按照自己的約定有了編輯模式青团,那么要實現(xiàn)編輯效果:刪除數(shù)據(jù)譬巫,增加數(shù)據(jù)就得看5這個方法了,這里我貼一下我的代碼督笆,實現(xiàn)虎撲體育的刪除和插入效果:
#pragma mark - tableView編輯模式:刪除和插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"commitEditingStyle");
if (editingStyle == UITableViewCellEditingStyleDelete) {
//容易忽視的問題:刪除和插入 分別操作完芦昔,textArr和allTextArr數(shù)據(jù)操作別合在一起
//否則程序會崩潰
//刪除操作
NSUInteger index = indexPath.row;
NSString *text = [textArr objectAtIndex:index];
[textArr removeObjectAtIndex:index];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//插入操作
[allTextArr addObject:text];
NSIndexPath *insertToIndexPath = [NSIndexPath indexPathForRow:allTextArr.count-1 inSection:1];
[tableView insertRowsAtIndexPaths:@[insertToIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
//插入操作
NSUInteger index = indexPath.row;
NSString *text = [allTextArr objectAtIndex:index];
[textArr addObject:text];
NSIndexPath *insertToIndexPath = [NSIndexPath indexPathForRow:textArr.count-1 inSection:0];
[tableView insertRowsAtIndexPaths:@[insertToIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//刪除操作
[allTextArr removeObjectAtIndex:index];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
到這里,其實整個編輯模式基本講完了娃肿,但是咕缎!大家會發(fā)現(xiàn)你自己實現(xiàn)的代碼,上面section0的那些行不僅僅能在section0里移動料扰,還能夠移動到下面section1里凭豪,這不符合我們想要的效果!這種時候該怎么辦呢晒杈?嫂伞!
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
秘密就在上面這個方法,大家可以看看最終效果