資料多的時候我們會使用Table View,時常需要更新資料再來顯示,有時候一次只需要更新一則資料,此時我們也能指定特定資料來更新,若想讓使用者體驗好的話,還可以輕易地使用動畫呢!
過去的我不論要更新的資料有多少,都直接讓整個Table View重新載入,但其實可以只更新某幾則資料。
回想去年約兩個月搞定上海的超大案子有使用過,卻沒有記錄下使用方式,所以之後的案子都不會想使用,因為以為還要花時間研究~
所以啊~有時間的話最好稍微記錄一下之後會常用到的「技巧」,以便節省許多研究時間,因而能開發更有趣的功能!
/**
Theme: Reload Table View Animation
IDE: Xcode 6
Language: Objective C
Date: 104/05/23
Author: HappyMan
Blog: https://cg2010studio.wordpress.com/
*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[displayTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight, // slide in from right (or out to right)
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone, // available in iOS 3.0
UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you
};
我示範的是,在點擊某個row時,會重新載入該row,此時會發現有動畫呢!不會像之前沒動畫時,突然出現突然消失。
只要一行code就能實現!除了重載外,還有插入、刪除的時候可以使用動畫呢!目前SDK提供8種動畫方式。
參考:去年的我。

隨意留個言吧:)~