iOS开发小技巧:刷新UITableView
2015-11-25 19:37
288 查看
UITableView对于iOS开发者来说一定不会陌生,很有可能你的APP很多界面都用到它。关于UITableView的文章,想必已经不计其数,没事可以多看看。特别是UITableView优化的文章,非常值得仔细琢磨一番。
UITableView对于iOS开发者来说一定不会陌生,很有可能你的APP很多界面都用到它。关于UITableView的文章,想必已经不计其数,没事可以多看看。特别是UITableView优化的文章,非常值得仔细琢磨一番。
今天我们来看看如何刷新UITableView的,一般情况下,刷新UITableView,我们会直接调用reloadData方法。
reloadData是刷新整个UITableView,有时候,我们可能需要局部刷新。比如:只刷新一个cell、只刷新一个section等等。这个时候在调用reloadData方法,虽然用户看不出来,但是有些浪费资源。
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
这样就可以很方便的刷新第一个section的第一个cell。虽然看起来代码多了,但是确实比较节省资源。尽量少的刷新,也是UITableView的一种优化。
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
上面这段代码是刷新第0个section。
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
};
UITableView对于iOS开发者来说一定不会陌生,很有可能你的APP很多界面都用到它。关于UITableView的文章,想必已经不计其数,没事可以多看看。特别是UITableView优化的文章,非常值得仔细琢磨一番。
今天我们来看看如何刷新UITableView的,一般情况下,刷新UITableView,我们会直接调用reloadData方法。
刷新UITableView
[self.tableView reloadData];reloadData是刷新整个UITableView,有时候,我们可能需要局部刷新。比如:只刷新一个cell、只刷新一个section等等。这个时候在调用reloadData方法,虽然用户看不出来,但是有些浪费资源。
刷新局部cell
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
这样就可以很方便的刷新第一个section的第一个cell。虽然看起来代码多了,但是确实比较节省资源。尽量少的刷新,也是UITableView的一种优化。
局部刷新section
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
上面这段代码是刷新第0个section。
刷新动画
刷新UITableView还有几个动画: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
};
相关文章推荐
- Ant的build.xml脚本模板
- 053-18 What two statements are true regarding the recommendations received from the SQL Access Advi
- loadrunner :Insufficient records for parameter in table to provide the Vuser with unique data
- StringBuffer和StringBuilder的区别
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- 读书笔记--UI组件
- 【bzoj1819】[JSOI]Word Query电子字典 trie+暴力
- UI第十二天:通讯录实战
- LightOJ 1048 - Conquering Keokradong(二分)
- 自定义UIButton
- Request.Item和Request.Params
- hdu5568 sequence2(dp+大数)
- BIOS锁定纯UEFI启动的解锁办法
- StringBuilder与StringBuffer的区别(转) &精简版StringBuilder,提速字符串拼接
- UIWindow & UIWindowLevel详解
- 设计模式(五)创建者模式(Builder)
- UIColor (16进制颜色、透明色使用)
- KVC setvalue:forkey与setvalue:forkeypath的区别
- UIPickerView两列依附关系
- String & StringBuffer & StringBuilder