iOS中3DTouch預覽導致TableView滑動卡頓問題解決的方法
1.發(fā)現(xiàn)問題
今天一早來公司,一個同事舉著他的6p對我們說:“你看看這是嘛啊...怎么劃不動啊...”我一看,果然,滑兩下TableView,大概加載2頁多就卡飛了...頓時想以是他機子太老了,物理內(nèi)存不夠用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約束全部注釋掉后,問題依舊存在。先排除?。ùa略了)
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:...
}
內(nèi)存泄露:使用instrument監(jiān)控并測試后,除了幾個第三方SDK導致的Leek之外,并沒有自己調(diào)用方法產(chǎn)生的泄露。(本寶寶對instrument的使用還比較膚淺,后面會再仔細琢磨,大哥們勿噴...)先排除!

UShareSDK和XMPPFramework中有泄露
3.定位問題
既然導致TableView卡頓的幾大原因都排除了,那就要考慮額外的因素了。折騰這一頓后,靜下心來仔細回憶最近到底有沒有改過首頁的TableView。然后...好像...好像...前些天閑來無事我是在首頁加了一個3DTouch重按預覽的功能!難道...
懷著雞凍的心情仔細檢查了一遍3DTouch轉(zhuǎn)場代理方法,發(fā)現(xiàn)并木有什么問題:
#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;
}
}
然后就又迷茫了,到底問題在哪?上個廁所,噓噓一下,冷靜冷靜...果然,衛(wèi)生間是一個偉大的地方...提褲子的時候突然想到一個重大問題!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;
}
既然已經(jīng)發(fā)現(xiàn)根本問題所在了:因為每一次滑動都會在DataSource里面為當前Cell注冊一遍3DTouch代理并指定SourceView。那么不寫在DataSource返回Cell的方法里,寫哪里呢?
-didSelectedRowAtIndexPath?點擊時注冊?
-willSelectRowAtIndexPath?馬上點擊時注冊?
實驗之后發(fā)現(xiàn)不太好,這兩個TableView代理方法都只能在第一次點擊cell,push到下一個控制器之后才能使用預覽功能。因為這兩個方法調(diào)用的時機類似UIControlEventTouchUpInside(不嚴謹,只做一個比喻),必須抬手才可以觸發(fā),而我們的3DTouch是不需要抬手的。
4.解決問題
既然已經(jīng)確定了問題:因為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代理,方法內(nèi)部自動判定是否已經(jīng)設置過. @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;
}
然后我們只需要在數(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...
//...
}
當然,這里防止Cell多次注冊3DTouch的方法有很多,比如重寫DESIGNATED_INITIALIZER方法,通過代理實現(xiàn)等等。我這里使用抽基類+標識屬性實現(xiàn)也是圖一個簡單快速好實現(xiàn),歡迎各位大神指點更好的方法。

縱享絲滑
5.總結(jié)
作者的個人能力尚淺,這篇文章更多的是幫助初級、中級iOSer整理遇到類似問題的思路,一個3DTouch是小,積累類似經(jīng)驗是大。希望能和大家一起進步!
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS應用中存儲用戶設置的plist文件的創(chuàng)建與讀寫教程
這篇文章主要介紹了iOS應用中存儲用戶設置的plist文件的創(chuàng)建與讀寫教程,plist文件是在Xcode下的項目中會被自動生成,里面采用XML格式記錄數(shù)據(jù),需要的朋友可以參考下2016-04-04
iOS schem與Universal Link 調(diào)試時踩坑解決記錄
這篇文章主要為大家介紹了iOS schem與Universal Link 調(diào)試時踩坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

