虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > IOS编程/Objective-C > iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法

iOS中3DTouch预览导致TableView滑动卡顿问题解决的方法
类别:IOS编程/Objective-C   作者:码皇   来源:互联网   点击:

这篇文章主要给大家介绍了关于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 tableview卡顿 tableview滑动卡顿 ios