关于tableView点击cell中按钮进入排序模式(cell行高统一可长按排序)

RTDragCellTableView:  https://github.com/RustedBigB/RTDragCellTableView

首先,为三方的tableview类添加了一个isMove的BOOL属性,用于标记是否处于排序状态。

在三方tableview类里的长按的方法里判断isMove的值。如果不是排序状态直接return。

if (!self.isMove) {

        return;

    }

为自定义的cell声明一个代理,在点击cell中的按钮时,使用代理改变tableview的isMove的值,使tableview进入排序状态。

- (void)myTableViewCell:(MyTableViewCell *)cell funcBtnDidClick:(UIButton *)func {

#pragma mark -- 改变数据源数组;

     NSMutableArray *arrayM = [NSMutableArray array];

    //遍历数据源数组;

    for (NSInteger i = 0; i < self.data.count; i ++) {

        {//在这里是因为需要在排序模式下每一行的文字都需要能单独排序,

//所以在进入排序模式的时候对数据源做了一些处理。

        RTModel *model = self.data[i];

        if (model.title.length) {//判断数组对应位置的对象是不是文本属性;

            //对应位置是字符串需要进行处理;

            NSString *strA = model.title;

            while ([strA rangeOfString:@"\n"].length) {//存在\n;

                NSRange range= [strA rangeOfString:@"\n"];

                NSString *subStr = [strA substringToIndex:range.location];

                //加入数组;

                RTModel *newModel = [[RTModel alloc]init];

                newModel.title = subStr;

                [arrayM addObject:newModel];

                strA = [strA substringFromIndex:range.length+range.location];

            }

            //最后剩余的字符串不存在换行,所以需要单独添加。

            RTModel *lastModel = [[RTModel alloc]init];

            lastModel.title = strA;

            [arrayM addObject:lastModel];

}            

        }else {//不是文本属性直接加入的可变数组

            [arrayM addObject:model];

        }

    }

    //改变数据数组;

     self.data = arrayM.copy;

    self.isMove = YES;

    [_tableView reloadData];

    _tableView.isMove = YES;

}

在控制器的代理方法中刷新tableview,使cell的高度根据是否处于排序状态来返回固定值或者其他。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    RTModel *model = self.data[indexPath.row];

    UIFont *font = [UIFont systemFontOfSize:15];

    NSDictionary *attribute = @{NSFontAttributeName: font};

    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 0);

    CGSize retSize = [model.title boundingRectWithSize:size

                                             options:\

                      NSStringDrawingTruncatesLastVisibleLine |

                      NSStringDrawingUsesLineFragmentOrigin |

                      NSStringDrawingUsesFontLeading

                                          attributes:attribute

                                             context:nil].size;

    CGFloat h = retSize.height + 40;

//这里的高度计算纯粹为了演示,请按自己的需求返回。

    return self.isMove ? 40 : h ;

}

同理,在tableview的拖拽完成的代理方法里面将isMove的值改为no,继续刷新tableview。

- (void)cellDidEndMovingInTableView:(RTDragCellTableView *)tableView {

{

/*

在这里,如果不对数据源做处理排序完成后每一行文字就是一个cell,都会附带排序按钮,感觉上不是很好。

所以,在这里将相邻的cell模型如果是文字的内容就进行了合并,并在中间添加了“\n”使相邻的文本的cell成为一个cell。

*/

#pragma mark -- 移动完成后合并相邻的文字输入;

    NSMutableArray *arrayM = [NSMutableArray array];

    for (NSInteger i = 0; i < self.data.count; ) {

        RTModel *model = self.data[i];

        if (model.title.length) {

            NSString *strNew = model.title;

            while (++i < self.data.count&&[self.data[i] title].length) {

                RTModel *nextModel = self.data[i];

                strNew = [NSString stringWithFormat:@"%@\n%@",strNew,nextModel.title];

            }

            RTModel *newModel = [[RTModel alloc]init];

            newModel.title = strNew;

            [arrayM addObject:newModel];

            

        }else {//不是文本输入直接加入到新的可变数组;

            [arrayM addObject:model];

            i ++;

        }

    }

    self.data = arrayM.copy;

}

    self.isMove = NO;

    [_tableView reloadData];

    _tableView.isMove = NO;

}

 

http://git.oschina.net/ruiruiheshui/RTDragCellTableView_test

转载于:https://my.oschina.net/ruiruiheshui/blog/739856

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值