欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動(dòng)卡頓問題解決的方法

 更新時(shí)間:2018年03月27日 10:56:56   作者:一只iOS開發(fā)狗  
這篇文章主要給大家介紹了關(guān)于iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動(dòng)卡頓問題解決的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)同樣遇到的朋友們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。

1.發(fā)現(xiàn)問題

今天一早來公司,一個(gè)同事舉著他的6p對(duì)我們說:“你看看這是嘛啊...怎么劃不動(dòng)啊...”我一看,果然,滑兩下TableView,大概加載2頁多就卡飛了...頓時(shí)想以是他機(jī)子太老了,物理內(nèi)存不夠用balabala等等原因回懟時(shí)...人家后面又說了一句:“你看人家今日頭條怎么滑都沒事~”。

好吧,我看看好吧。


雖然是在iPhone X上錄的,但上下滑動(dòng)卡頓依舊非常明顯

2.排除問題

沒錯(cuò),我和你想的一樣,十有八九應(yīng)該是那幾個(gè)老問題導(dǎo)致的:

Cell高度計(jì)算問題:把同事寫的SD自動(dòng)計(jì)算行高寫死后問題依舊存在。先排除!

///行高寫死后依舊卡頓
- (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約束全部注釋掉后,問題依舊存在。先排除?。ùa略了)

Cell沒有被TableView注冊復(fù)用:檢查并更換DataSource方法后,確認(rèn)注冊、復(fù)用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復(fù)用方法,效果依舊卡頓,證明TableViewCell復(fù)用沒有問題。
  JstyleNewsOnePlusImageArticleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  if (!cell) {
   cell = [[JstyleNewsOnePlusImageArticleViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  }
  */
JstyleNewsOnePlusImageArticleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
  ///劇透:卡頓的原因就在這!Cell重復(fù)注冊3DTouch預(yù)覽!后面會(huì)說解決辦法。
  [self registerForPreviewingWithDelegate:self sourceView:cell];
  if (indexPath.row < self.dataArray.count) {
   cell.model = model;
  }
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  return cell;
  } //else if...
 //case 2:...
}

內(nèi)存泄露:使用instrument監(jiān)控并測試后,除了幾個(gè)第三方SDK導(dǎo)致的Leek之外,并沒有自己調(diào)用方法產(chǎn)生的泄露。(本寶寶對(duì)instrument的使用還比較膚淺,后面會(huì)再仔細(xì)琢磨,大哥們勿噴...)先排除!


UShareSDK和XMPPFramework中有泄露

3.定位問題

既然導(dǎo)致TableView卡頓的幾大原因都排除了,那就要考慮額外的因素了。折騰這一頓后,靜下心來仔細(xì)回憶最近到底有沒有改過首頁的TableView。然后...好像...好像...前些天閑來無事我是在首頁加了一個(gè)3DTouch重按預(yù)覽的功能!難道...
懷著雞凍的心情仔細(xì)檢查了一遍3DTouch轉(zhuǎn)場代理方法,發(fā)現(xiàn)并木有什么問題:

#pragma mark - 3DTouch預(yù)覽
- (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;
 }
}

然后就又迷茫了,到底問題在哪?上個(gè)廁所,噓噓一下,冷靜冷靜...果然,衛(wèi)生間是一個(gè)偉大的地方...提褲子的時(shí)候突然想到一個(gè)重大問題!3DTouch預(yù)覽是需要提前注冊代理并告知控制器SourceView是誰的!而這個(gè)注冊方法...好像有點(diǎn)子問題:

- (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];
  }
  //?。。∈撬撬褪撬。?!每一次滑動(dòng)TableView復(fù)用Cell的時(shí)候都會(huì)注冊一遍3DTouch代理!不卡才怪了!
  //[self registerForPreviewingWithDelegate:self sourceView:cell];注釋掉之后,瞬間“縱享絲滑”!

  if (indexPath.row < self.dataArray.count) {
   cell.model = model;
  }
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  return cell;
}

既然已經(jīng)發(fā)現(xiàn)根本問題所在了:因?yàn)槊恳淮位瑒?dòng)都會(huì)在DataSource里面為當(dāng)前Cell注冊一遍3DTouch代理并指定SourceView。那么不寫在DataSource返回Cell的方法里,寫哪里呢?

-didSelectedRowAtIndexPath?點(diǎn)擊時(shí)注冊?

-willSelectRowAtIndexPath?馬上點(diǎn)擊時(shí)注冊?

實(shí)驗(yàn)之后發(fā)現(xiàn)不太好,這兩個(gè)TableView代理方法都只能在第一次點(diǎn)擊cell,push到下一個(gè)控制器之后才能使用預(yù)覽功能。因?yàn)檫@兩個(gè)方法調(diào)用的時(shí)機(jī)類似UIControlEventTouchUpInside(不嚴(yán)謹(jǐn),只做一個(gè)比喻),必須抬手才可以觸發(fā),而我們的3DTouch是不需要抬手的。

4.解決問題

既然已經(jīng)確定了問題:因?yàn)镃ell重復(fù)注冊了3DTouch,那么如何只讓每個(gè)Cell只注冊一遍呢?上面廢話說太多啦!直接上代碼:

//
// JstyleNewsBaseTableViewCell.h
// JstyleNews
//
// Created by 王磊 on 2018/1/25.
// Copyright © 2018年 JstyleNews. All rights reserved.
//
///抽取一個(gè)BaseCell基類,后面的子類Cell只需繼承
#import <UIKit/UIKit.h>
@interface JstyleNewsBaseTableViewCell : UITableViewCell
///是否設(shè)置過3DTouch代理
@property (nonatomic, assign , readonly) BOOL isAllreadySetupPreviewingDelegate;

/**
 給當(dāng)前Cell設(shè)置3DTouch代理,方法內(nèi)部自動(dòng)判定是否已經(jīng)設(shè)置過.
 
 @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 ()
///標(biāo)識(shí)當(dāng)前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;
}

然后我們只需要在數(shù)據(jù)源方法里面簡單的調(diào)一下方法就完啦:

- (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...
  //...
}

當(dāng)然,這里防止Cell多次注冊3DTouch的方法有很多,比如重寫DESIGNATED_INITIALIZER方法,通過代理實(shí)現(xiàn)等等。我這里使用抽基類+標(biāo)識(shí)屬性實(shí)現(xiàn)也是圖一個(gè)簡單快速好實(shí)現(xiàn),歡迎各位大神指點(diǎn)更好的方法。


縱享絲滑

5.總結(jié)

作者的個(gè)人能力尚淺,這篇文章更多的是幫助初級(jí)、中級(jí)iOSer整理遇到類似問題的思路,一個(gè)3DTouch是小,積累類似經(jīng)驗(yàn)是大。希望能和大家一起進(jìn)步!

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論