iOS开发 - 三级目录,点击有展开及收缩效果

项目中需要写三级目录,要求点击有展开及收缩的效果。

  • 效果图如下:
三级目录

思路:整体肯定是一个 UITableView,由于是三级目录,这里用 tableHeaderView 与 sectionHeader 的意义不是很大,因此全部使用 cell。还有两个重要问题需要预想到,一是如何区分这个 cell 是一级、二级,还是三级的,二是如何区分这个 cell 是展开的还是收缩的,因此我这里写了一个模型类,里面定义了一些属性来解决这些问题。

  • 核心代码一,这段代码是模仿网络请求的,其主要作用是给数据模型赋值。
    // 一级
    for (NSInteger i = 0; i < array.count; i++) {
        QQLevelModel *model = [[QQLevelModel alloc] init];
        model.depict = array[i][@"depict"];
        model.firstIndex = i+1;
        [self.dataArray addObject:model];
        // 二级
        NSArray *secondLevelData = array[i][@"secondLevelData"];
        for (NSInteger j = 0; j < secondLevelData.count; j++) {
            QQLevelModel *model = [[QQLevelModel alloc] init];
            model.depict = secondLevelData[j][@"depict"];
            model.firstIndex = i+1;
            model.secondIndex = j+1;
            [self.dataArray addObject:model];
            NSArray *thirdLevelData = secondLevelData[j][@"thirdLevelData"];
            // 三级
            [thirdLevelData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                QQLevelModel *model = [[QQLevelModel alloc] init];
                model.firstIndex = i+1;
                model.secondIndex = j+1;
                model.thirdIndex = idx+1;
                model.depict = obj[@"depict"];
                [self.dataArray addObject:model];
            }];
        }
    }
  • 核心代码二,这段代码是 cell 的点击事件,当点击一级二级的 cell 时,UITableView 会出现展示或收缩的效果。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QQLevelModel *current_model = self.dataModel.dataArray[indexPath.row];
    // 如果点击的是一级cell
    if (current_model.secondIndex == 0 && current_model.thirdIndex == 0) {
        current_model.isOpen = !current_model.isOpen;
        NSMutableArray *indexPathArray = @[].mutableCopy;
        [self.dataModel.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            QQLevelModel *model = obj;
            BOOL is_belong = model.firstIndex == current_model.firstIndex;    //是否在当前分组
            BOOL is_current = model.secondIndex == 0;                           //是否是一级cell
            //如果在当前分组下,并且不是一级cell,则全部隐藏,关闭
            if (is_belong && !is_current) {
                model.isShow = !current_model.isOpen;
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
                [indexPathArray addObject:indexPath];
            }
        }];
        [self reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationFade];
        return;
    }
    
    // 如果点击的是二级cell
    if (current_model.thirdIndex == 0) {
        NSMutableArray *indexPathArray = @[].mutableCopy;
        [self.dataModel.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            QQLevelModel *model = obj;
            BOOL is_belong = model.secondIndex == current_model.secondIndex && model.firstIndex == current_model.firstIndex;  //是否在当前分组
            BOOL is_current = model.thirdIndex == 0;                           // 是否是二级cell
            //如果在当前分组下,并且不是二级cell,则全部隐藏
            if (is_belong && !is_current) {
                model.isShow = !model.isShow;
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
                [indexPathArray addObject:indexPath];
            }
        }];
        [self reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationFade];
        return;
    }
}

demo地址,整体的代码逻辑并不复杂,欢迎各位同行一起交流学习。

你可能感兴趣的:(iOS开发 - 三级目录,点击有展开及收缩效果)