UITableView出现卡顿如何处理
tableView的beginUpdate和endUpdate要比reloadData和reloadRowsAtIndexPaths好,因为beginUpdate和endUpdate会执行一个动画block,图片加载的时候显的很平滑。你自己试一下就知道了。
加载图片的时候要用多线程,要用缓存,也就是需要异步加载
计算cell的高度的时候要尽量的简单,因为tableVIew中cell的高度是一次性加载完的
要用重用机制,一定要用,不然会卡的
用户习惯性快速的滚动,视图和数据内容都会快速的变化,如果效率问题处理不好,很容易有卡顿的现象。造成用户体验的降低。
数据刷新
如果我们的modle更新了。相应的要体现到UITableView上面。简单的我们可以reload整个TableView。这样做很方便,而且数据上没有问题。唯一的问题就是,reload整个TableView的效率太低了。而且,往往我们只是少数的Cell内容变化。所以没有必要去reload整个TableView。而是那条数据变化去刷新对应的Cell就好了。这样做效率提高很多。
具体涉及到的几个函数
- (void)beginUpdates;
- (void)endUpdates; - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
我自己遇到的情况:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell; switch (indexPath.row) {
case :
// 初始化抢红包Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_rob forIndexPath:indexPath];
[self setupRobRedPackageCell:cell];
break;
case :
// 初始化轮播Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_ad forIndexPath:indexPath];
[self setupAdCarouseCell:cell];
break;
case :
// 初始化趣味答题Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_answer forIndexPath:indexPath];
[self setupInterestingAnswerCell:cell];
break;
case :
// 初始化排行榜Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_charts forIndexPath:indexPath];
[self setupChartsCell:cell];
break;
case :
// 初始化折扣优惠区Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_discount forIndexPath:indexPath];
[self setupDiscountCell:cell];
break;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
其中的[self setupxxxx]用来创建和添加各种视图到Cell的contentView中, 由于没有判断仔细, 导致每次重用cell的时候都会调用到, 所以照成了卡顿, 以下是修改后的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell; switch (indexPath.row) {
case :
// 初始化抢红包Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_rob forIndexPath:indexPath];
if (self.cellReuseTag >= ) break;
[self setupRobRedPackageCell:cell];
self.cellReuseTag = ;
break;
case :
// 初始化轮播Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_ad forIndexPath:indexPath];
if (self.cellReuseTag >= ) break;
[self setupAdCarouseCell:cell];
self.cellReuseTag = ;
break;
case :
// 初始化趣味答题Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_answer forIndexPath:indexPath];
if (self.cellReuseTag >= ) break;
[self setupInterestingAnswerCell:cell];
self.cellReuseTag = ;
break;
case :
// 初始化排行榜Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_charts forIndexPath:indexPath];
if (self.cellReuseTag >= ) break;
[self setupChartsCell:cell];
self.cellReuseTag = ;
break;
case :
// 初始化折扣优惠区Cell
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_discount forIndexPath:indexPath];
if (self.cellReuseTag >= ) break;
[self setupDiscountCell:cell];
self.cellReuseTag = ;
break;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
简单的, 也就是搞多一个标记来记录cell已经初始化过, 跳过初始化的步骤
UITableView出现卡顿如何处理的更多相关文章
- UITableView cell复用出错问题 页面滑动卡顿问题 & 各杂七杂八问题
UITableView 的cell 复用机制节省了内存,但是有时对于多变的自定义cell,重用时会出现界面出错(例如复用出错,出现cell混乱重影).滑动卡顿等问题,这里只简单敲下几点复用出错时的解决 ...
- 记一次简单的UITableView卡顿优化
先说需求,要做一个类似这种的列表 标签控件直接用的第三方 YZTagList 不知道的可以去搜一下,当这不重要. 重要的是这个控件加载数据的时候非常影响列表滑动效果,造成卡顿,尤其是列表行数如果更多的 ...
- Vegas常见问题解答,如何处理预览卡顿
制作视频并不是简单的拼拼凑凑,很多时候我们都需要给视频加上一些视频特效或转场等效果,如果只是图片素材的话,还不会出现卡顿的现象,但是当你给视频添加了效果后,在预览窗口看到的就是非常卡顿了.除了本身计算 ...
- 第3月30天 UIImage imageWithContentsOfFile卡顿 Can't add self as subview MPMoviePlayerControlle rcrash
1. UIImage imageWithContentsOfFile卡顿 [[UIImage alloc] initWithContentsOfFile 卡顿 2.uitableview scroll ...
- 想让安卓app不再卡顿?看这篇文章就够了
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由likunhuang发表于云+社区专栏 实现背景 应用的使用流畅度,是衡量用户体验的重要标准之一.Android 由于机型配置和系统的 ...
- iOS之tableView性能优化/tableView滑动卡顿?
本文围绕以下几点展开tableView性能优化的论述? 1.UITableViewCell重用机制? 2.tableView滑动为什么会卡顿? 3.优化方法? 4.总结 1.UITableViewCe ...
- xamarin MasterDetailPage点击Master时卡顿现象
在很多项目中经常会使用到MasterDetailPage的布局方式,而且一般做为主页面来开发,在开发中,发现一个并不算Bug的问题,但是却发生了,以此记录下来,方便大家探讨. 现象是这样的,我开发了一 ...
- 解决UINavigationController在pushViewController时出现的"卡顿"问题
进行开发中,遇到了个小问题: 在使用UINavigationController的-pushViewController:animated:执行入栈一个子控制器操作时(即最新栈顶子控制器),会出现推出 ...
- webstorm卡顿问题
近期随着项目开展,文件逐渐增大,webstrom频繁出现卡顿,而且时有崩溃现象,提示没有足够的内存来执行请求的操作,需要增加Xms设置. 解决办法: 1.找到WebStorm.exe.vmoption ...
随机推荐
- 学生选课系统数据库SQL语句考试题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- C# 基于委托的事件
事件基于多播委托的特性. 多播委托事实上就是一组类型安全的函数指针管理器,调用则执行顺序跳转到数组里所有的函数指针里执行. class Program { public class CarInfoEv ...
- Logger之Logger.getLogger(CLass)
之前一直在使用System.out.println()来调试.但是用这种方式开发项目部署到生产环境,会因为众多的控制台输出降低应用的性能.这时候Log4J就成为可平衡开发和部署应用的利器了. 在项目中 ...
- some words that I always make mistake
发音相似容易混淆的词汇 alteration 英 [ɔːltə'reɪʃ(ə)n; 'ɒl-] 美 [,ɔltə'reʃən] n. 修改,改变:变更 alteration /ˌɔːltəˈre ...
- 自定义Qt按钮
转自:http://blog.csdn.net/starcloud_zxt/article/details/5185556 Qt自带的PushButton样式比较单一,在开发的时候往往按钮的形状各异, ...
- POJ2513 Colored Sticks(欧拉)
题目链接. 题目大意: 给很多木棍,两端被涂了颜色.任意两根木棍的相同颜色处可以拼接在一起,问有没有可能将所有的木棍都连起来,成一条直线? 分析: 考点,欧拉道路. 将一根木棍看成一条边,两端的颜色看 ...
- [Design Pattern] Filter Pattern 简单案例
Filter Pattern,即过滤模式,通过不同的过滤标准,或者低耦合将过滤标准组合在一起,对一组对象进行过滤,属于结构类的设计模式. 下面是一个过滤模式的简单案例. Criteria 定义过滤接口 ...
- xshell中启动linux图形界面
使用root用户执行xhost + IP为客户端机器IP,使用远程登录用户执行 DISPLAY=IP:0.0;export DISPLAY; 使用远程登录的用户执行: xhost +
- jquery and js 判断一个元素是否存在
一.javascript中判断一个元素是否存在 if(document.getElementById('example')){ // do sth } 二.jquery中判断一个元素是否存在 < ...
- Ubuntu 13.04 安装 GCC4.8.1
终于有了完整实现C++11的GCC 4.8.1. 给自己的系统升级吧. 下面的步骤可以安装GCC4.8.1, 内容来自:http://askubuntu.com/questions/312620/ho ...