美文网首页
UITableViewCell拖动排序

UITableViewCell拖动排序

作者: rookiesss | 来源:发表于2020-06-15 16:03 被阅读0次

    UITableViewCell拖动排序功能系统本身就有的,不过系统的只能长按一个按钮才能拖动,如何实现整行可以长按拖动呢?
    思路简单,将系统的长按view改变大小铺满cell就行。

    1.找到cell的拖动view。
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
        [super setEditing:editing animated:animated];
        if (_moveView) return;
        NSString *type = @"UITableViewCellReorderControl";
        for (UIView * view in self.subviews) {
            if ([NSStringFromClass([view class]) isEqualToString:type] || [NSStringFromClass([view class]) rangeOfString: @"Reorder"].location != NSNotFound) {
                view.frame = CGRectMake(0, 0, S_Width, 60);
                for (UIView *imageView in view.subviews) {
                    //隐藏系统的拖动icon,如果需要的隐藏的话
                    imageView.hidden = YES;
                }
                _moveView = view;
                [self loadUIWithView:_moveView];
            } else {
                //隐藏不必要UI元素,否则会影响其他控件交互区域
                view.hidden = YES;
            }
        }
    }
    
    

    这里需要注意的是这个视图会在setEditing方法调用之后才会添加到cell,在cell初始化时还没有的,而setEditing方法需要开启tableView.editing = YES。

    2.以获取到的_moveView作为backView添加控件就行(注意控件要添加到_moveView上)。

    在cell重用过程中会出现控件偏移的情况,可以这样:

    - (void)layoutSubviews {
        [super layoutSubviews];
        _moveView.frame = CGRectMake(0, 0, S_Width, 60);
    }
    

    cell编辑模式会有删除icon等,可以这样:

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellEditingStyleNone;
    }
    

    相关文章

      网友评论

          本文标题:UITableViewCell拖动排序

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