iphone - 如何从 UITableView 中删除部分 (iPhone/iPad)

标签 iphone ipad uitableview

我正在尝试想出一种从 TableView 中删除部分的好方法(在编码和外观方面)。理想情况下,我想创建一种删除部分的方法,该方法类似于删除 UITableViewCell 的过程(即按下时显示红色删除按钮的红色减号按钮)。

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

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

提前致谢。

最佳答案

实现您自己的 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

关于iphone - 如何从 UITableView 中删除部分 (iPhone/iPad),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10834431/

相关文章:

objective-c - 无法在表格 View 中显示图像

iphone - 计算不同时区对应的日期和时间

iphone - UIButton 标题上方的中心图像

ios - CGGradient 在 iPad 1 和 2 上显示不正确

iphone - NSURLConnection didFailWithError 未调用?

ios - IndexPathForCell 和 IndexPathForRowAtPoint 崩溃

iphone - 我们在哪里可以看到您的 iPhone 应用程序的总下载量?

ios - 如何在 IOS 中将日期格式化为字符串,如 "One Days Ago","Minutes Ago"?

ios - iPad 多任务支持需要这些方向

ios - 使表格 View 部分快速展开