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

 更新时间:2018年03月27日 10:56:56   作者:一只iOS开发狗  
这篇文章主要给大家介绍了关于iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法,文中通过示例代码介绍的非常详细,对同样遇到的朋友们具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。

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是小,积累类似经验是大。希望能和大家一起进步!

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

相关文章

  • iOS Xcode升级Xcode15报错SDK does not contain 'libarclite'

    iOS Xcode升级Xcode15报错SDK does not contain

    这篇文章主要为大家介绍了iOS Xcode 升级Xcode15报错: SDK does not contain 'libarclite'解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • iOS自定义日期和数据源选择控件

    iOS自定义日期和数据源选择控件

    这篇文章主要为大家详细介绍了iOS自定义日期和数据源选择控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • 详解 iOS 系统中的视图动画

    详解 iOS 系统中的视图动画

    这篇文章主要介绍了iOS 系统中的视图动画的的相关资料,帮助大家更好的理解和学习使用ios开发,感兴趣的朋友可以了解下
    2021-02-02
  • iOS图片实现可拉伸不变形的处理操作

    iOS图片实现可拉伸不变形的处理操作

    这篇文章主要为大家详细介绍了iOS图片实现可拉伸不变形的处理操作,通过UIImage对象调用该方法,并且传入要拉伸的图片的名字作为参数,实现返回一个可拉伸不变形的图片,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • iOS滑动全屏实现返回功能

    iOS滑动全屏实现返回功能

    这篇文章主要为大家详细介绍了iOS滑动全屏实现返回功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 用iOS模拟器安装App的方法

    用iOS模拟器安装App的方法

    下面小编就为大家分享一篇用iOS模拟器安装App的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • iOS获取当前设备WiFi信息的方法

    iOS获取当前设备WiFi信息的方法

    很多公司现在都在做免费WIFI,车站、公交、地铁、餐厅,只要是人员密集流动的地方就有WIFI,免费WIFI从最初的网页认证方式也逐渐向客户端认证方式偏移。本文主要介绍iOS获取当前设备WiFi信息的方法,有需要的可以参考借鉴。
    2016-09-09
  • ios开发UITableViewCell图片加载优化详解

    ios开发UITableViewCell图片加载优化详解

    这篇文章主要为大家介绍了ios开发UITableViewCell图片加载优化的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • iOS自定义UIButton点击动画特效

    iOS自定义UIButton点击动画特效

    这篇文章主要为大家详细介绍了iOS自定义UIButton点击动画特效,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • IOS URL中文乱码问题解决方案

    IOS URL中文乱码问题解决方案

    这篇文章主要介绍了IOS 解决URL中文乱码问题解决方案的相关资料,需要的朋友可以参考下
    2016-10-10

最新评论