既然已经发现根本问题所在了:因为每一次滑动都会在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...
//...
}










