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 ...
随机推荐
- laravel框架——composer导入laravel
第一种: composer create-project --prefer-dist laravel/laravel 名称 "5.2.*"第二种: composer global ...
- Android Service 启动和停止服务
activity_main.xml 定义两个Button控件,start_service和stop_service. <LinearLayout xmlns:android="http ...
- linux c redirect 重定向
用execvp实现时,运行adb,如果adb 服务没有启动,会启动adb服务,启动adb服务时,pipe返回的管道在读的时候堵塞了. 查看了popen的源码,发现popen是用sh -c来执行的,避免 ...
- shell中条件判断if中的-z到-d的意思【转载】
本文转载自[http://blog.csdn.net/utstarm/article/details/6536916] [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] ...
- 关于C#泛型列表List<T>的基本用法总结
//示例代码如下:using System;using System.Collections.Generic;using System.Collections.ObjectModel;namespac ...
- jQuery官网一个关于菜单的例子
来源地址:https://my.oschina.net/xngiser/blog/28323 <ul id="menu"> <li class="men ...
- 二、Linux文件系统之内存管理
虚拟内存 32位:4G 64位:2^64 内存管理: 进程管理 自动分配和管理 支持模块化程序设计 保护和访问控制 长期存储 虚拟内存 <---MMU-->物理内存
- NSURLConnection获取数据
- (void)loadDataFromUrl { NSURL* url = [NSURL URLWithString:@"http://m.weather.com.cn/data/1011 ...
- SQLite3基本使用从shell到python
SQLite是一个轻量级的关系型数据库,在訪问量不超过10万PV的中小站点中使用绰绰有余. 并且使用方便,接口简单,以下从命令行和python接口双方面介绍SQLite3的基本操作. 在linux终端 ...
- hash表的创建
功能:创建一个hash table.假设有处理冲突,则採用再散列法放置该元素 代码參考<零基础学数据结构> 代码例如以下: root@ubuntu:/mnt/shared/appbox/h ...