如何删除静态单元格表​​格中的部分

问题描述:

我试图删除或隐藏表格中的静态单元格。我试图隐藏功能viewDidLoad。这里是代码:如何删除静态单元格表​​格中的部分

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

    [self.tableView reloadData]; 
} 

仍然出现部分。我正在使用故事板。你能帮我吗?谢谢!

看来,reloadData使表视图重新读取dataSource。在致电reloadData之前,您还应该从dataSource中移除数据。如果您正在使用阵列,请在致电reloadData之前删除您想要的对象removeObject:

查看这里找到的答案。 How to remove a static cell from UITableView using Storyboards当使用静态单元格时,似乎是一个问题的烦恼者。希望这可以帮助。

我发现通过重写numberOfRowsInSection来隐藏段是最方便的。

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

这适合我的目的,但这样做后仍然有一点垂直空间。 – guptron 2013-04-29 22:19:50

在这里,你去。这一个也删除垂直空间。

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; 
}