最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

好吧,我看看好吧。


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

2.排除問(wèn)題

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

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

///行高寫(xiě)死后依舊卡頓
- (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上子控件大小位置異步渲染問(wèn)題:把Cell上所有同事寫(xiě)的SDLayout約束全部注釋掉后,問(wèn)題依舊存在。先排除?。ùa略了)

Cell沒(méi)有被TableView注冊(cè)復(fù)用:檢查并更換DataSource方法后,確認(rèn)注冊(cè)、復(fù)用cell沒(méi)有問(wèn)題。先排除!

- (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ù)用沒(méi)有問(wèn)題。
  JstyleNewsOnePlusImageArticleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  if (!cell) {
   cell = [[JstyleNewsOnePlusImageArticleViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  }
  */
JstyleNewsOnePlusImageArticleViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
  ///劇透:卡頓的原因就在這!Cell重復(fù)注冊(cè)3DTouch預(yù)覽!后面會(huì)說(shuō)解決辦法。
  [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)控并測(cè)試后,除了幾個(gè)第三方SDK導(dǎo)致的Leek之外,并沒(méi)有自己調(diào)用方法產(chǎn)生的泄露。(本寶寶對(duì)instrument的使用還比較膚淺,后面會(huì)再仔細(xì)琢磨,大哥們勿噴...)先排除!


UShareSDK和XMPPFramework中有泄露

3.定位問(wèn)題

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

然后就又迷茫了,到底問(wèn)題在哪?上個(gè)廁所,噓噓一下,冷靜冷靜...果然,衛(wèi)生間是一個(gè)偉大的地方...提褲子的時(shí)候突然想到一個(gè)重大問(wèn)題!3DTouch預(yù)覽是需要提前注冊(cè)代理并告知控制器SourceView是誰(shuí)的!而這個(gè)注冊(cè)方法...好像有點(diǎn)子問(wè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ì)注冊(cè)一遍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)根本問(wèn)題所在了:因?yàn)槊恳淮位瑒?dòng)都會(huì)在DataSource里面為當(dāng)前Cell注冊(cè)一遍3DTouch代理并指定SourceView。那么不寫(xiě)在DataSource返回Cell的方法里,寫(xiě)哪里呢?

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

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

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

4.解決問(wèn)題

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

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

/**
 給當(dāng)前Cell設(shè)置3DTouch代理,方法內(nèi)部自動(dòng)判定是否已經(jīng)設(shè)置過(guò).
 
 @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是否注冊(cè)過(guò)
@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ù)源方法里面簡(jiǎn)單的調(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多次注冊(cè)3DTouch的方法有很多,比如重寫(xiě)DESIGNATED_INITIALIZER方法,通過(guò)代理實(shí)現(xiàn)等等。我這里使用抽基類(lèi)+標(biāo)識(shí)屬性實(shí)現(xiàn)也是圖一個(gè)簡(jiǎn)單快速好實(shí)現(xiàn),歡迎各位大神指點(diǎn)更好的方法。


縱享絲滑

5.總結(jié)

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

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

相關(guān)文章

最新評(píng)論

嵊泗县| 弋阳县| 西林县| 民县| 宾阳县| 雅江县| 湖州市| 尉氏县| 利津县| 灵石县| 贡山| 济源市| 尚志市| 多伦县| 上林县| 静宁县| 宜君县| 庆阳市| 巴林右旗| 松桃| 仙居县| 平度市| 平远县| 珠海市| 利辛县| 九龙县| 垫江县| 龙山县| 库车县| 察哈| 景泰县| 吉木乃县| 屯留县| 楚雄市| 呈贡县| 宜昌市| 奉化市| 南召县| 松江区| 五大连池市| 故城县|