tableView操作刷新塊的應(yīng)用
在介紹動畫塊之前缔俄,我們先看幾個函數(shù):
插入分區(qū)
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
animation參數(shù)是一個枚舉,枚舉的動畫類型如下
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,//淡入淡出
UITableViewRowAnimationRight,//從右滑入
UITableViewRowAnimationLeft,//從左滑入
UITableViewRowAnimationTop,//從上滑入
UITableViewRowAnimationBottom,//從下滑入
UITableViewRowAnimationNone, ?//沒有動畫
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100 ?// 自動選擇合適的動畫
};
刪除分區(qū)
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
重載一個分區(qū)
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ;
移動一個分區(qū)
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
插入一些行
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
刪除一些行
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
重載一些行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
移動某行
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
了解了上面幾個函數(shù),我們來看什么是操作刷新塊:
當(dāng)我們調(diào)用的上面的函數(shù)時证逻,tableView會立刻調(diào)用代理方法進(jìn)行刷新逸贾,如果其中我們所做的操作是刪除某行宠互,而然數(shù)據(jù)源數(shù)組我們可能并沒有刷新浴讯,程序就會崩潰掉擎厢,原因是代理返回的信息和我們刪除后不符
IOS為我們提供了下面兩個函數(shù)解決這個問題:
開始塊標(biāo)志
- (void)beginUpdates;
結(jié)束快標(biāo)志
- (void)endUpdates;
我們可以將我們要做的操作全部寫在這個塊中究流,那么,只有當(dāng)程序執(zhí)行到結(jié)束快標(biāo)志后动遭,才會調(diào)用代理刷新方法代碼示例如下:
[tab beginUpdates];
[tab deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[dataArray removeObjectAtIndex:1];
[tab endUpdates];
注意:不要在這個塊中調(diào)用reloadData這個方法芬探,它會使動畫失效