【问题标题】:How to delete sections from static cell tableview如何从静态单元格表​​格视图中删除部分
【发布时间】:2011-12-15 09:57:48
【问题描述】:

我正在尝试删除或隐藏 tableview 中带有静态单元格的部分。我试图将它隐藏在函数viewDidLoad 中。代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView beginUpdates];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES];
    [self.tableView endUpdates];

    [self.tableView reloadData];
}

仍然显示部分。我在其中使用故事板。你能帮帮我吗?谢谢!

【问题讨论】:

    标签: objective-c tableview storyboard


    【解决方案1】:

    我发现通过覆盖 numberOfRowsInSection 来隐藏部分最方便。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if ( section == 1 )
            // Hide this section
            return 0;
        else
            return [super tableView:self.tableView numberOfRowsInSection:section];
    }
    

    【讨论】:

    • 这符合我的目的,但这样做后仍有一点垂直空间。
    • 完全符合 OP 的要求,伟大的灵魂!
    【解决方案2】:

    检查此处找到的答案。 How to remove a static cell from UITableView using Storyboards 使用静态单元格时似乎是个问题。希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      给你。这也消除了垂直空间。

      NSInteger sectionToRemove = 1;
      CGFloat distance = 10.0f; // minimum is 2 since 1 is minimum for header/footer
      BOOL removeCondition; // set in viewDidLoad
      
      /**
       *  You cannot remove sections.
       *  However, you can set the number of rows in a section to 0,
       *  this is the closest you can get.
       */
      
      - (NSInteger)tableView:(UITableView *)tableView
       numberOfRowsInSection:(NSInteger)section {
        if (removeCondition && section == sectionToRemove)
          return 0;
        else
          return [super tableView:self.tableView numberOfRowsInSection:section];
      }
      
      /**
       *  In this case the headers and footers sum up to a longer
       *  vertical distance. We do not want that.
       *  Use this workaround to prevent this:
       */
      - (CGFloat)tableView:(UITableView *)tableView
          heightForFooterInSection:(NSInteger)section {
      
        return removeCondition &&
                       (section == sectionToRemove - 1 || section == sectionToRemove)
                   ? distance / 2
                   : distance;
      }
      
      - (CGFloat)tableView:(UITableView *)tableView
          heightForHeaderInSection:(NSInteger)section {
      
        return removeCondition &&
                       (section == sectionToRemove || section == sectionToRemove + 1)
                   ? distance / 2
                   : distance;
      }
      

      【讨论】:

        【解决方案4】:

        似乎reloadData 使表视图重新读取数据源。您还应该在调用reloadData 之前从数据源中删除数据。如果您使用的是数组,请在调用 reloadData 之前使用 removeObject: 删除您想要的对象。

        【讨论】:

          猜你喜欢
          • 2021-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多