gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:04:12 26 4
gpt4 key购买 nike

我正在尝试想出一种从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com