当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

1.发现问题

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

好吧,我看看好吧。


虽然是在iphone x上录的,但上下滑动卡顿依旧非常明显

2.排除问题

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

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

///行高写死后依旧卡顿
- (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没有问题。先排除!

- (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的使用还比较肤浅,后面会再仔细琢磨,大哥们勿喷...)先排除!


usharesdk和xmppframework中有泄露

3.定位问题

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

#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是谁的!而这个注册方法...好像有点子问题:

- (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只注册一遍呢?上面废话说太多啦!直接上代码:

//
// 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
//
// 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;
}

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

- (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方法,通过代理实现等等。我这里使用抽基类+标识属性实现也是图一个简单快速好实现,欢迎各位大神指点更好的方法。


纵享丝滑

5.总结

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网