iOS

tableview禁止交互 但是允许滑动

2018-07-24  本文已影响253人  无敌大闸蟹

项目中有个需求 详情页自己进去时是可以编辑的 也就是tableview上有输入框 输入框也是可以用的 但是其他身份的角色也能进详情页 只能看 不给摸 嘿嘿嘿 。。。。。

上代码 继承tableview的类 重写其中的几个方法


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        _cellUserInteractionEnabled = YES;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {
       _cellUserInteractionEnabled = YES;
    }
    return self;
}

- (instancetype)init
{
    if (self = [super init]) {
        _cellUserInteractionEnabled = YES;
    }
    return self;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//    NSLog(@"%@",gestureRecognizer.superclass);
    if (_cellUserInteractionEnabled) {
        return YES;
    }
    else {
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
            if ([touch.view isKindOfClass:[UITextView class]]) {
                ((UITextView *)touch.view).editable = NO;
            }
            else if ([touch.view isKindOfClass:[UIButton class]]) {
                ((UIButton *)touch.view).enabled = NO;
            }
            return YES;
        }
        else {
            return NO;
        }
    }
}

- (void)setTableFooterView:(UIView *)tableFooterView
{
    if (_cellUserInteractionEnabled) {
        [super setTableFooterView:tableFooterView];
    }
    else {
        [super setTableFooterView:[UIView new]];
    }
}

- (void)setCellUserInteractionEnabled:(BOOL)cellUserInteractionEnabled
{
    _cellUserInteractionEnabled = cellUserInteractionEnabled;
    if (!_cellUserInteractionEnabled) {
        [super setTableFooterView:[UIView new]];
    }
}


cellUserInteractionEnabled 是.h暴露的BOOL属性 默认为YES 当你不想tableview有交互 但是能滑动的时候 tableView的对象的cellUserInteractionEnabled设置为NO就行了

上一篇下一篇

猜你喜欢

热点阅读