2012-05-31 63 views
4

我试图想出一个很好的(在编码&外观)从表视图删除一节的方式。理想情况下,我想创建一种删除与删除UITableViewCell(即按下时显示红色删除按钮的红色减号按钮)相似的部分的方法。如何从UITableView(iPhone/iPad)删除部分

这个问题是否有预先存在的解决方案?我尝试了一些研究,但我真的找不到任何有用的东西。如果没有预先存在的解决方案,我认为我可以尝试并实施一个解决方案。我的计划是为每个部分创建一个自定义标题。在自定义标题中,我会添加删除按钮,这些按钮的作用与删除行时的方式相同。人们认为这会起作用吗?

所以要清楚,我在问是否存在删除UITableView中的部分的现有解决方案。如果没有,我想知道人们是否认为我的想法是可行的,或者是否有任何我忽略的问题。

在此先感谢。

回答

8

实现自己headerView是要走的路。 Apple不提供用于删除部分的内置用户界面。如果你拉下iOS设备的通知区域,你会明白如何使它看起来不错。

实现也不是很复杂。我使用的是这样的:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 21.0f; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 21)]; 
    headerView.backgroundColor = [UIColor lightGrayColor]; 

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, 250, 21)]; 
    headerLabel.text = [NSString stringWithFormat:@"Section %d", section]; 
    headerLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]; 
    headerLabel.textColor = [UIColor whiteColor]; 
    headerLabel.backgroundColor = [UIColor lightGrayColor]; 
    [headerView addSubview:headerLabel]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.tag = section + 1000; 
    button.frame = CGRectMake(300, 2, 17, 17); 
    [button setImage:[UIImage imageNamed:@"31-circle-x"] forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [headerView addSubview:button]; 
    return headerView; 
} 

- (IBAction)deleteButtonPressed:(UIButton *)sender { 
    NSInteger section = sender.tag - 1000; 
    [self.objects removeObjectAtIndex:section]; 
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic]; 

    // reload sections to get the new titles and tags 
    NSInteger sectionCount = [self.objects count]; 
    NSIndexSet *indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, sectionCount)]; 
    [self.tableView reloadSections:indexes withRowAnimation:UITableViewRowAnimationNone]; 
} 

enter image description here

+0

这里是什么self.objects? –

5

是存在可用于删除tableview.You部分可以使用以下方法现有的方法:

[m_TableView beginUpdates]; 
[m_TableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop]; 
[m_TableView endUpdates]; 

但你必须保持一件事记住,当你删除部分你必须修改:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

因为现在你的节数由1

1

减少如果我是正确的,苹果只提供行中的UITableView编辑模式下删除。

我能想出3种添加按钮的方法来删除特定的部分。

首先,除了一段UITableView之外还放了一个按钮,它不在UITableView之内(我不认为你可以在UITableView之内加上一个按钮,或者你可以但不用不知道该怎么做)。您可以检查UITableView是否处于可编辑模式,并相应地显示或隐藏您的按钮。

Seond,如果您确实想要UITableView中的删除按钮,您可以创建cuntom UITableViewCell并将单元格中的部分删除按钮。这样你每个UITableViewCell都会有一个按钮,这并不是很棒。

第三选择不会提供任何按钮,只是让用户以Apple默认方式删除单元格。你所做的只是跟踪你的数组中剩下多少个用于填充该部分的对象。如果计数达到0,则删除该部分。

这是我之前没有做类似的事情:

我有一个NSMutableArray firstLevelArray里面包含每个包含一些对象几个NSMutableArray secondLevelArray的。因此,每个secondLevelArray将为一个节提供数据,secondLevelArray中的对象将为每个单元提供数据。在- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView,我做return [firstLevelArray count];何地,只要我想删除某个部分,[firstLevelArray removeObjectAtIndex:someIndex]后,我做[myTableView reloadData]

希望这有助于