美文网首页
3D Touch PeeK And Pop导致TableView

3D Touch PeeK And Pop导致TableView

作者: KnowWhy | 来源:发表于2019-01-19 13:41 被阅读0次

    概述

    在获取复用Cell方法中注册3D Touch Peek And Pop,导致滑动越来越卡顿。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
        if (@available(iOS 9.0, *)) {
            if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
                [self registerForPreviewingWithDelegate:self sourceView:cell];
            }
        }
        return cell;
    }
    

    解决方案

    在复用Cell中添加一个是否已注册3D Touch Peek And Pop的属性register3DTouch标识,如已注册就不重复注册。

    @interface BaseTableViewCell : UITableViewCell
    @property (nonatomic,getter = isRegister3DTouch) BOOL register3DTouch; //cell是否注册3D Touch标识
    - (void)registerPreviewingWithController:(UIViewController<UIViewControllerPreviewingDelegate> *)controller;
    @end
        
    @implementation BaseTableViewCell
    
    - (void)registerPreviewingWithController:(UIViewController<UIViewControllerPreviewingDelegate> *)controller
    {
        if (@available(iOS 9.0, *)) {
            if (controller.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable && !self.isRegister3DTouch) {
                [controller registerForPreviewingWithDelegate:controller sourceView:self];
                self.register3DTouch = YES;
            }
        }
    }
    
    @en
    

    获取复用Cell中替换注册的API

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
        if (@available(iOS 9.0, *)) {
            if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
                [cell registerPreviewingWithController:self];
            }
        }
        return cell;
    }
    

    相关文章

      网友评论

          本文标题:3D Touch PeeK And Pop导致TableView

          本文链接:https://www.haomeiwen.com/subject/yjwadqtx.html