OC-开发案例收集

iOS UITableViewCell 的拖动排序

2019-07-09  本文已影响0人  写_Bug_小能手
cellSort.gif

前言:

需求:类似于考试试卷的编辑,从题库中选取若干道题目,进行分类(如:选择题、填空题等),每个类别之间可以排序,类别下面对应的题目可以排序,但是不同类别之间的题目不可相互排序。
虽然我知道苹果提供了一个可以移动cell排序的代理方法,但是我对于分组的类别排序还是很懵逼。。。。

// tableView 移动排序的代理方法
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath ;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

想法

于是我想了一个比较取巧的方法,数组按照类别来分组显示,每个section的第一行作为类别名称来显示,除此之类都显示具体类别对应的内容(题目)。
1、当我拖动的sourceIndexPath和我将要放的目标的destinationIndexPath的section相同且sourceIndexPath的row和destinationIndexPath的row不同时为0时,做组内的内容拖动排序。
2、当我拖动的sourceIndexPath和我将要放的目标的destinationIndexPath的section不相同且sourceIndexPath的row为0时,做分组之间的拖动排序。

做法

1、数据模型

// 数据模型结构
@interface SortModel : NSObject

@property (nonatomic, copy) NSString *nameStr;
@property (nonatomic, copy) NSString *typeStr;
@property (nonatomic, copy) NSArray<NSString *> *question;

@property (nonatomic, assign, getter=isSelect) BOOL select;
@end


// controller中整合数据
- (NSMutableArray *)sourceArr {

    if (!_sourceArr) {

        NSArray *array = @[@[@"哈哈", @"嘿嘿", @"哼哼"],
                           @[@"啦啦", @"巴巴", @"嗯嗯", @"哦哦", @"捂捂", @"呜呜", @"污污"],
                           @[@"捂捂", @"呜呜", @"污污"],
                           @[@"捂捂", @"呜呜", @"污污"],
                           @[@"捂捂", @"呜呜", @"污污"],
                           @[@"捂捂", @"呜呜", @"污污"]];
        _sourceArr = [NSMutableArray array];
        for (int i = 0; i < array.count; i++) {

            NSArray *arr = array[i];
            NSMutableArray *data = [NSMutableArray array];
            for (int j = 0; j < arr.count; j++) {

                [data addObject:arr[j]];
            }
            SortModel *model = [[SortModel alloc] init];
            model.select   = NO;
            model.question = data;
            model.typeStr  = [NSString stringWithFormat:@"type %d", i];
            [_sourceArr addObject:model];
        }
    }
    return _sourceArr;
}

2、当点击的row为0时,修改对应modelselect属性为!model.select,并刷新对应分组,若该属性为YES该组显示行数为model.question.count + 1,反之行数则为1

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        SortModel *model = self.sourceArr[indexPath.section];
        model.select = !model.select;
        
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
        [tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];;
    }
}

3、cell 的拖动排序

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return tableView.editing;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    // 是否为同组排序
    if (sourceIndexPath.section == destinationIndexPath.section) {
        // 是不是第一个row(即无法和分类标题交换)
        if (sourceIndexPath.row != 0 && destinationIndexPath.row != 0) {
            
            SortModel *sourceModel = self.sourceArr[sourceIndexPath.section];
            NSString  *sourceStr   = sourceModel.question[sourceIndexPath.row - 1];
            NSMutableArray *array  = [NSMutableArray arrayWithArray:sourceModel.question];
            
            [array removeObject:sourceStr];
            [array insertObject:sourceStr atIndex:destinationIndexPath.row - 1];
            sourceModel.question = array;
            [self.sourceArr replaceObjectAtIndex:sourceIndexPath.section withObject:sourceModel];
        }
        
    } else {
        // 不同组,且row为0,则为分类的排序,对数据进行处理
        if (sourceIndexPath.row == 0) {
            
            SortModel *model = self.sourceArr[sourceIndexPath.section];
            [self.sourceArr removeObject:model];
            
            [self.sourceArr insertObject:model atIndex:destinationIndexPath.section];
        }
    }
    [tableView reloadData];
}

总结

以上就是我的关于分组排序和分组内容排序的方法,但是还是感觉不太好,尤其是展开分类时对分类进行排序那块。以后有机会在想着优化,当然各位大佬如果有好的办法还请不吝赐教呀。

上一篇下一篇

猜你喜欢

热点阅读