uitableview:嵌套的节标题 [英] uitableview: nested section headers

查看:62
本文介绍了uitableview:嵌套的节标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下结构实现uitableview:

Im trying to implement a uitableview with the following structure:

  • 第0组
    • 第0节
      • 单元格0
      • 单元格1
      • 单元2
      • section group 0
        • section 0
          • cell 0
          • cell 1
          • cell 2
          • 单元格0
          • 单元格1
          • 单元2
          • 第0节
            • 单元格0
            • 单元格1
            • 单元2
            • section 0
              • cell 0
              • cell 1
              • cell 2
              • 单元格0
              • 单元格1
              • 单元2

              ,它应该像下面的屏幕截图一样(1-2-3-4): http://dl.dropbox.com/u/2213241/uitableview.png

              and it should scoll like this screenshot (1-2-3-4): http://dl.dropbox.com/u/2213241/uitableview.png

              所以总是有两个部分可见.

              So always two sections are visible.

              我该如何实施?还是有人已经实现了?

              How do i implement this? Or has anyone implemented this already?

              谢谢:)

              推荐答案

              具有嵌套节的技巧是在表视图中具有两种行.一个代表表格的第二层,另一个代表表格视图中的普通行.假设您有一个两级数组(例如节)来表示表视图中的项目.

              The trick to have nested sections is to have two kinds of rows in the table view. One to represent the second level of sections and another to represent the normal rows in the tableview. Let's say you have a two level array (say sections) to represent the items in your table view.

              然后,我们拥有的部分总数仅是顶级部分的数目.每个顶层部分中的行数将是子部分数+每个子部分中的行数.

              Then, the total number of sections that we have are just the number of top level sections. The number of rows in each top level section would be the number of subsections + the number of rows in each subsection.

              - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
                  return self.sections.count;
              }
              
              - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
                  NSArray *sectionItems = self.sections[(NSUInteger) section];
                  NSUInteger numberOfRows = sectionItems.count; // For second level section headers
                  for (NSArray *rowItems  in sectionItems) {
                      numberOfRows += rowItems.count; // For actual table rows
                  }
                  return numberOfRows;
              }
              

              现在,我们需要考虑的就是如何为表视图创建行.在情节提要中使用不同的重用标识符设置两个原型,一个用于节标题,另一个用于行项目,然后根据数据源方法中的要求实例化正确的原型.

              Now, all we need to think about is how to create the rows for the table view. Set up two prototypes in the storyboard with different reuse identifiers, one for the section header and another for row item and just instantiate the correct one based on the asked index in the data source method.

              - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                  NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
                  NSMutableArray *sectionHeaders = self.sectionHeaders[(NSUInteger) indexPath.section];
                  NSIndexPath *itemAndSubsectionIndex = [self computeItemAndSubsectionIndexForIndexPath:indexPath];
                  NSUInteger subsectionIndex = (NSUInteger) itemAndSubsectionIndex.section;
                  NSInteger itemIndex = itemAndSubsectionIndex.row;
              
                  if (itemIndex < 0) {
                      // Section header
                      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SECTION_HEADER_CELL" forIndexPath:indexPath];
                      cell.textLabel.text = sectionHeaders[subsectionIndex];
                      return cell;
                  } else {
                      // Row Item
                      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ROW_CONTENT_CELL" forIndexPath:indexPath];
                      cell.textLabel.text = sectionItems[subsectionIndex][itemIndex];
                      return cell;
                  }
              }
              
              - (NSIndexPath *)computeItemAndSubsectionIndexForIndexPath:(NSIndexPath *)indexPath {
                  NSMutableArray *sectionItems = self.sections[(NSUInteger) indexPath.section];
                  NSInteger itemIndex = indexPath.row;
                  NSUInteger subsectionIndex = 0;
                  for (NSUInteger i = 0; i < sectionItems.count; ++i) {
                      // First row for each section item is header
                      --itemIndex;
                      // Check if the item index is within this subsection's items
                      NSArray *subsectionItems = sectionItems[i];
                      if (itemIndex < (NSInteger) subsectionItems.count) {
                          subsectionIndex = i;
                          break;
                      } else {
                          itemIndex -= subsectionItems.count;
                      }
                  }
                  return [NSIndexPath indexPathForRow:itemIndex inSection:subsectionIndex];
              }
              

              此处是有关如何执行此操作的详细帖子.

              这篇关于uitableview:嵌套的节标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆