在UITableView中插入动态节标题

时间:2013-04-10 05:33:05

标签: iphone uitableview

我有一个UITableView,我想在用户单击添加按钮时动态添加节标题。如果用户不需要,用户也应该能够删除它们。在每个标题下,用户应该能够添加相关的项目列表。除此之外,用户应该只能在选定的部分下动态插入行。请提出一些实现此功能的想法。提前谢谢。

3 个答案:

答案 0 :(得分:2)

您可以通过删除然后再添加该部分来实现此目的,这将调用tableView:titleForHeaderInSection:

假设您的部分索引为0:

BOOL shouldShowHeader;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    shouldShowHeader ? @"Your Header" : nil;
}

- (IBAction)buttonAction:(id)sender
{
    shouldShowHeader = !shouldShowHeader;
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
    [self.tableView beginUpdates];
    [self.tableView deleteSections:set withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView insertSections:set withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
}

页脚的工作方式相同。

答案 1 :(得分:0)

我将告诉动态部分标题。保持一个标志并最初设置为0。当用户单击该按钮时按钮方法将标志设置为1并重新加载tableView。

          -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
          {
                  if(flag)
                       return aView;//alloc init aView and return.
                  else
                       return nil.
          }

类似地,动态行有一个数组,numberOfRows是array.count。 单击按钮在阵列中再插入一个项目并重新加载表。

我希望这有帮助。

答案 2 :(得分:0)

当用户点击此方法中的按钮时,设置一个标志并重新加载您的tableview,然后执行此操作..

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(flag)
    {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 30)];
        return headerView;
    }
    else
        return nil;
}

你可以像这样删除表格视图中的标题......

如果要删除表的标题视图,只需将myTable.tableHeaderView属性设置为nil即可。如果您拥有的实际上是一个节头,那么您需要从viewForHeaderInSection方法返回nil并调用[myTableView reloadData]

我希望这会很好。