服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法

iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法

2021-04-22 18:12一只iOS开发狗 IOS

这篇文章主要给大家介绍了关于iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法,文中通过示例代码介绍的非常详细,对同样遇到的朋友们具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。

1.发现问题

今天一早来公司,一个同事举着他的6p对我们说:“你看看这是嘛啊...怎么划不动啊...”我一看,果然,滑两下tableview,大概加载2页多就卡飞了...顿时想以是他机子太老了,物理内存不够用balabala等等原因回怼时...人家后面又说了一句:“你看人家今日头条怎么滑都没事~”。

好吧,我看看好吧。

iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法
虽然是在iphone x上录的,但上下滑动卡顿依旧非常明显

2.排除问题

没错,我和你想的一样,十有八九应该是那几个老问题导致的:

cell高度计算问题:把同事写的sd自动计算行高写死后问题依旧存在。先排除!

?
1
2
3
4
5
///行高写死后依旧卡顿
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath {
 //return [self.tableview cellheightforindexpath:indexpath model:self.dataarray[indexpath.row] keypath:@"model" cellclass:[jstylenewsoneplusimagevideoviewcell class] contentviewwidth:kscreenwidth];
return 200;
}

cell上子控件大小位置异步渲染问题:把cell上所有同事写的sdlayout约束全部注释掉后,问题依旧存在。先排除!(代码略了)

cell没有被tableview注册复用:检查并更换datasource方法后,确认注册、复用cell没有问题。先排除!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
 //省略部分防崩溃判断代码...
 jstylenewshomepagemodel *model = self.dataarray[indexpath.row];
 switch ([model.type integervalue]) {
 case 1:{
  if ([model.head_type integervalue] == 1 && [model.isimagearticle integervalue] == 1) {
  static nsstring *id = @"jstylenewsoneplusimagearticleviewcell";
  /*换一种cell复用方法,效果依旧卡顿,证明tableviewcell复用没有问题。
  jstylenewsoneplusimagearticleviewcell *cell = [tableview dequeuereusablecellwithidentifier:id];
  if (!cell) {
   cell = [[jstylenewsoneplusimagearticleviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id];
  }
  */
jstylenewsoneplusimagearticleviewcell *cell = [tableview dequeuereusablecellwithidentifier:id forindexpath:indexpath];
  ///剧透:卡顿的原因就在这!cell重复注册3dtouch预览!后面会说解决办法。
  [self registerforpreviewingwithdelegate:self sourceview:cell];
  if (indexpath.row < self.dataarray.count) {
   cell.model = model;
  }
  cell.selectionstyle = uitableviewcellselectionstylenone;
  return cell;
  } //else if...
 //case 2:...
}

内存泄露:使用instrument监控并测试后,除了几个第三方sdk导致的leek之外,并没有自己调用方法产生的泄露。(本宝宝对instrument的使用还比较肤浅,后面会再仔细琢磨,大哥们勿喷...)先排除!

iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法
usharesdk和xmppframework中有泄露

3.定位问题

既然导致tableview卡顿的几大原因都排除了,那就要考虑额外的因素了。折腾这一顿后,静下心来仔细回忆最近到底有没有改过首页的tableview。然后...好像...好像...前些天闲来无事我是在首页加了一个3dtouch重按预览的功能!难道...
怀着鸡冻的心情仔细检查了一遍3dtouch转场代理方法,发现并木有什么问题:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma mark - 3dtouch预览
- (uiviewcontroller *)previewingcontext:(id<uiviewcontrollerpreviewing>)previewingcontext viewcontrollerforlocation:(cgpoint)location {
 nsindexpath *indexpath = [self.tableview indexpathforcell:(uitableviewcell* )[previewingcontext sourceview]];
 if ([self.dataarray[indexpath.row] isimagearticle].integervalue == 1) {
  jstylepicturetextviewcontroller *picturevc = [[jstylepicturetextviewcontroller alloc] init];
  if (indexpath.row < self.dataarray.count) {
   picturevc.rid = [self.dataarray[indexpath.row] id];
   cgrect rect = cgrectmake(0, 0, self.view.frame.size.width,[self.tableview cellforrowatindexpath:indexpath].height);
   previewingcontext.sourcerect = rect;
  }
  return picturevc;
  
 } else {
  jstylenewsarticledetailviewcontroller *detailvc = [[jstylenewsarticledetailviewcontroller alloc] init];
  detailvc.preferredcontentsize = cgsizemake(0.0f,500.0f);
  if (indexpath.row < self.dataarray.count) {
   detailvc.rid = [self.dataarray[indexpath.row] id];
   detailvc.titlemodel = self.detaildataarray[indexpath.row];
   cgrect rect = cgrectmake(0, 0, self.view.frame.size.width,[self.tableview cellforrowatindexpath:indexpath].height);
   previewingcontext.sourcerect = rect;
  }
  return detailvc;
 }
}

然后就又迷茫了,到底问题在哪?上个厕所,嘘嘘一下,冷静冷静...果然,卫生间是一个伟大的地方...提裤子的时候突然想到一个重大问题!3dtouch预览是需要提前注册代理并告知控制器sourceview是谁的!而这个注册方法...好像有点子问题:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
  static nsstring *id = @"jstylenewsoneplusimagearticleviewcell";
  jstylenewsoneplusimagearticleviewcell *cell = [tableview dequeuereusablecellwithidentifier:id];
  if (!cell) {
   cell = [[jstylenewsoneplusimagearticleviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id];
  }
  //!!!是他是他就是他!!!每一次滑动tableview复用cell的时候都会注册一遍3dtouch代理!不卡才怪了!
  //[self registerforpreviewingwithdelegate:self sourceview:cell];注释掉之后,瞬间“纵享丝滑”!
 
  if (indexpath.row < self.dataarray.count) {
   cell.model = model;
  }
  cell.selectionstyle = uitableviewcellselectionstylenone;
  return cell;
}

既然已经发现根本问题所在了:因为每一次滑动都会在datasource里面为当前cell注册一遍3dtouch代理并指定sourceview。那么不写在datasource返回cell的方法里,写哪里呢?

-didselectedrowatindexpath?点击时注册?

-willselectrowatindexpath?马上点击时注册?

实验之后发现不太好,这两个tableview代理方法都只能在第一次点击cell,push到下一个控制器之后才能使用预览功能。因为这两个方法调用的时机类似uicontroleventtouchupinside(不严谨,只做一个比喻),必须抬手才可以触发,而我们的3dtouch是不需要抬手的。

4.解决问题

既然已经确定了问题:因为cell重复注册了3dtouch,那么如何只让每个cell只注册一遍呢?上面废话说太多啦!直接上代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
// jstylenewsbasetableviewcell.h
// jstylenews
//
// created by 王磊 on 2018/1/25.
// copyright © 2018年 jstylenews. all rights reserved.
//
///抽取一个basecell基类,后面的子类cell只需继承
#import <uikit/uikit.h>
@interface jstylenewsbasetableviewcell : uitableviewcell
///是否设置过3dtouch代理
@property (nonatomic, assign , readonly) bool isallreadysetuppreviewingdelegate;
 
/**
 给当前cell设置3dtouch代理,方法内部自动判定是否已经设置过.
 
 @param controller 代理控制器
 */
- (void)setuppreviewingdelegatewithcontroller:(uiviewcontroller<uiviewcontrollerpreviewingdelegate> *)controller;
@end
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
// jstylenewsbasetableviewcell.m
// jstylenews
//
// created by 王磊 on 2018/1/25.
// copyright © 2018年 jstylenews. all rights reserved.
//
 
#import "jstylenewsbasetableviewcell.h"
@interface jstylenewsbasetableviewcell ()
///标识当前cell是否注册过
@property (nonatomic, assign) bool isallreadysetuppreviewingdelegate;
@end
 
@implementation jstylenewsbasetableviewcell
 
- (void)setuppreviewingdelegatewithcontroller:(uiviewcontroller<uiviewcontrollerpreviewingdelegate> *)controller {
 if (self.isallreadysetuppreviewingdelegate == yes) {
  return;
 }
 if ([self respondstoselector:@selector(traitcollection)]) {
  if ([self.traitcollection respondstoselector:@selector(forcetouchcapability)]) {
   if (self.traitcollection.forcetouchcapability == uiforcetouchcapabilityavailable) {
    [controller registerforpreviewingwithdelegate:controller sourceview:self];
    self.isallreadysetuppreviewingdelegate = yes;
   } else {
    self.isallreadysetuppreviewingdelegate = no;
   }
  }
 }
}
 
- (bool)isallreadysetuppreviewingdelegate {
 return _isallreadysetuppreviewingdelegate;
}

然后我们只需要在数据源方法里面简单的调一下方法就完啦:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
 //防崩溃代码省略...
 jstylenewshomepagemodel *model = self.dataarray[indexpath.row];
 switch ([model.type integervalue]) {
  case 1:{
   if ([model.head_type integervalue] == 1 && [model.isimagearticle integervalue] == 1) {
    static nsstring *id = @"jstylenewsoneplusimagearticleviewcell";
    jstylenewsoneplusimagearticleviewcell *cell = [tableview dequeuereusablecellwithidentifier:id];
    if (!cell) {
     cell = [[jstylenewsoneplusimagearticleviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id];
    }
    
    ///就是这里
    [cell setuppreviewingdelegatewithcontroller:self];
 
    if (indexpath.row < self.dataarray.count) {
     cell.model = model;
    }
    cell.selectionstyle = uitableviewcellselectionstylenone;
    
    return cell;
   } //else if...
  //...
}

当然,这里防止cell多次注册3dtouch的方法有很多,比如重写designated_initializer方法,通过代理实现等等。我这里使用抽基类+标识属性实现也是图一个简单快速好实现,欢迎各位大神指点更好的方法。

iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法
纵享丝滑

5.总结

作者的个人能力尚浅,这篇文章更多的是帮助初级、中级ioser整理遇到类似问题的思路,一个3dtouch是小,积累类似经验是大。希望能和大家一起进步!

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/4f6c22f2c5cd

延伸 · 阅读

精彩推荐
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22