IOS开发中实现分组样式的操作方法

来源:爱站网时间:2020-07-22编辑:网友分享
在项目开发中,因为项目需求可能需要设置分组的分组样式,这种实现方法有几种,其实只要我们的思路和逻辑简单,应该很很快进行操作和实现,别着急,下面是爱站技术频道小编为大家带来的IOS开发中实现分组样式的操作方法,参考下文看看吧!

在项目开发中,因为项目需求可能需要设置分组的分组样式,这种实现方法有几种,其实只要我们的思路和逻辑简单,应该很很快进行操作和实现,别着急,下面是爱站技术频道小编为大家带来的IOS开发中实现分组样式的操作方法,参考下文看看吧!

思路

思路很简单,对模型数据操作或则控制界面显示

先看下json部分数据

"chapterDtoList": [{
      "token": null,
      "id": 1295,
      "chapterName": "第一章",
      "parentId": 0,
      "chapterLevel": 0,
      "attachmentUrl": "",
      "description": null,
      "startDateTimestamp": null,
      "endDateTimestamp": null,
      "startDate": 1490889600000,
      "endDate": 1491062400000,
      "browseCount": 0,
      "workId": null,
      "chapterStatus": 3,
      "hadRead": 0,
      "subChapterList": [{
        "token": null,
        "id": 1296,
        "chapterName": "第一节",
        "parentId": 1295,
        "chapterLevel": 1,
        "attachmentUrl": "",
        "description": null,
        "startDateTimestamp": null,
        "endDateTimestamp": null,
        "startDate": null,
        "endDate": null,
        "browseCount": 0,
        "workId": null,
        "chapterStatus": null,
        "hadRead": 0,
        "subChapterList": [],
        "classUserReadInfo": []
      },

这种数据对应的一般都是个tableView, 然后根据章节分开,最终界面如下:

分析

这里采用UITableViewStylePlain样式,chapterDtoList对应章,subChapterList对应节。章的话我们使用headerView来做,节的话我们使用cell来做。然后只需要给headerView添加一个点击手势,点击的时候给对应的模型添加标识,从而去控制章节的收合。

方法一:

对模型数组进行操作,我们可以将返回的json数据转化为两个模型数组chapterListArray和tempChapterListArray,通过控制subChapterList的count来实现。界面的模型数据统一使用tempChapterListArray,展开与合并就等价于是否将“章数组“中的”节数组“赋值为nil

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  YJTOnlineTaskDetailModel *onlineTaskDetailModel = self.tempChapterListArray[section];
  return onlineTaskDetailModel.subChapterList.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  YJTOnlineChapeterCell *headerView = [tableView dequeueReusableCellWithIdentifier:onlineChapeterCell];
  YJTOnlineTaskDetailModel *onlineTaskDetailModel = self.chapterListArray[section];
  headerView.backgroundColor = [UIColor whiteColor];
  headerView.onlineTaskDetailModel = onlineTaskDetailModel;
  if (section == 0) {
    headerView.tipsLableHeight.constant = 30;
  }else {
    headerView.tipsLableHeight.constant = 0;
  }
  [headerView whenTapWithBlock:^{
    onlineTaskDetailModel.isSelected = !onlineTaskDetailModel.isSelected;
    YJTOnlineTaskDetailModel *detailModel = self.tempChapterListArray[section];
    if (detailModel.subChapterList == nil) {
      detailModel.subChapterList = onlineTaskDetailModel.subChapterList;
    }else {
      detailModel.subChapterList = nil;
    }
    [self.tableView reloadData];
  }];
  return headerView;
}

方法二:

上面的方法是通过控制模型数组来实现的,我们也可以采用控制界面的显示,从而达到我们的要求。既然我们在点击HeadView的时候已经标记过对应的模型数据是否展开,那么我们完全可以通过控制界面对应分组的个数来实现。当然也可以通过控制rowHeight来到达效果。相比之下,该方法简单明了些。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  YJTOnlineTaskDetailModel *onlineTaskDetailModel = self.chapterListArray[section];
  return onlineTaskDetailModel.isSelected ? onlineTaskDetailModel.subChapterList.count : 0;
}
 [headerView whenTapWithBlock:^{
    onlineTaskDetailModel.isSelected = !onlineTaskDetailModel.isSelected;
    [self.tableView reloadData];
  }];

以上所述是爱站技术频道小编为大家介绍的IOS开发中实现分组样式的操作方法,如果遇到操作错误或者疑问,欢迎在下方留言,我们第一时间和你一起解决。

上一篇:详细讲解swift单例的实现方法

下一篇:详解IOS开发之动画开发

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载