iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動(dòng)卡頓問題解決的方法
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ì)腳本之家的支持。
- 模仿iOS版微信的滑動(dòng)View效果
- iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
- iOS開發(fā)上下滑動(dòng)UIScrollview隱藏或者顯示導(dǎo)航欄的實(shí)例
- ios scrollview嵌套tableview同向滑動(dòng)的示例
- iOS基于UIScrollView實(shí)現(xiàn)滑動(dòng)引導(dǎo)頁
- Android仿IOS ViewPager滑動(dòng)進(jìn)度條
- IOS仿今日頭條滑動(dòng)導(dǎo)航欄
- iOS滑動(dòng)解鎖、滑動(dòng)獲取驗(yàn)證碼效果的實(shí)現(xiàn)代碼
- iOS 頁面滑動(dòng)與標(biāo)題切換顏色漸變的聯(lián)動(dòng)效果實(shí)例
- iOS自定義View實(shí)現(xiàn)卡片滑動(dòng)
相關(guān)文章
iOS應(yīng)用中存儲(chǔ)用戶設(shè)置的plist文件的創(chuàng)建與讀寫教程
這篇文章主要介紹了iOS應(yīng)用中存儲(chǔ)用戶設(shè)置的plist文件的創(chuàng)建與讀寫教程,plist文件是在Xcode下的項(xiàng)目中會(huì)被自動(dòng)生成,里面采用XML格式記錄數(shù)據(jù),需要的朋友可以參考下2016-04-04
iOS 自定義狀態(tài)欄和導(dǎo)航欄詳細(xì)介紹
這篇文章主要介紹了iOS 自定義狀態(tài)欄和導(dǎo)航欄詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11
iOS schem與Universal Link 調(diào)試時(shí)踩坑解決記錄
這篇文章主要為大家介紹了iOS schem與Universal Link 調(diào)試時(shí)踩坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

