[英]toggle a UIButton on the UITableView section header when table in edit mode
我有一个带有UILabel
和UIButton
的自定义视图,该视图用于分组的UITableView
节标题,如下所示:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
sectionHeaderView *headerView = [[sectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[headerView.label setText:[[self tableView:tableView titleForHeaderInSection:section] uppercaseString]];
if (_tableView.editing)
[headerView.button setHidden:NO];
return headerView;
}
如您所见,当UITableView
处于编辑模式时,我想取消隐藏按钮。
我使用UITableView
类别方法返回visibleHeaders,以便无需调用reloadData或reloadSections:withRowAnimation即可刷新可见节的标题视图。 该方法基本上返回一个NSNumber对象数组,并且效果很好。
这是将UITableView
设置为编辑模式的方法:
- (void)tapThatEditButtonBaby
{
[_tableView setEditing:YES animated:YES];
for (NSNumber *sectionNumber in _tableView.visibleHeaders) {
NSInteger section = [sectionNumber integerValue];
sectionHeaderView *headerView = (sectionHeaderView *)[self tableView:_tableView viewForHeaderInSection:section];
[headerView setBackgroundColor:[UIColor redColor]]; // highlighting view for testing.
}
}
解决这个问题的一种好方法是什么? 我认为理想情况下,我希望能够从UITableView
调用诸如updateSectionAndFooterAtIndex:之类的东西,但是没有这样的公开方法... :(
UPDATE
抱歉,您没有上一节的标题。 因此,针对您的情况的解决方案几乎相同。 您只需将NSDictionary与节索引一起存储为您感兴趣的键和按钮以获取价值。 这本字典将被您填充-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)部分,如下所示:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//...
[self.buttonsToHideInEditMode setObject:headerView.button forKey:@(section)];
}
并在您的-(void)tapThatEditButtonBaby中:
- (void)tapThatEditButtonBaby {
...
[self.buttonsToHideInEditMode enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL *stop) {
obj.hidden = !_tableView.editing);
}];
}
上一个答案
实际上,您有两个(或更多:))基本选项:
1)创建用于创建页眉/页脚视图的单独方法,例如:
- (UIView *)tableHeaderView
{
sectionHeaderView *headerView = [[sectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[headerView.label setText:[[self tableView:tableView titleForHeaderInSection:section] uppercaseString]];
if (_tableView.editing)
[headerView.button setHidden:NO];
return headerView;
}
//same way for -(UIView *)tableFooterView;
然后,只要您想更新/创建页眉/页脚,只需致电:
_tableView.tableFooterView = [self tableFooterView];
//or
_tableView.tableHeaderView = [self tableHeaderView];
2)在您的情况下,如果您只想根据编辑状态更改按钮,则可以只为按钮创建属性,并在需要时进行更改。
实际上,我发现问题的奥秘在于,如果直接对视图属性进行更改,则对节标题视图的视觉更改不会得到更新。
如果我加入这个方法setEditing:动画:在我sectionHeaderView
:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
if (animated) {
[UIView animateWithDuration:0.25 animations:^{
if (editing) {
[label setFrame:CGRectMake(50, 15, 200, 20)];
[button setFrame:CGRectMake(10, 12, 25, 25)];
}
else {
[label setFrame:CGRectMake(15, 15, 200, 20)];
[button setFrame:CGRectMake(-30, 12, 25, 25)];
}
}];
}
}
基本上,以下情况发生在我的tapThatEditButtonBaby方法中:
- (void)tapThatEditButtonBaby
{
[_tableView setEditing:YES animated:YES];
for (NSNumber *sectionNumber in _tableView.visibleHeaders) {
NSInteger section = [sectionNumber integerValue];
sectionHeaderView *headerView = (sectionHeaderView *)[self tableView:_tableView viewForHeaderInSection:section];
[sectionHeaderView setEditing:YES animated:YES]; // these visual changes work
[sectionHeaderView.label setFrame:....]; // these changes don't...
}
}
编辑: UITableView
自动将setEditing:animated发送到实现它的层次结构中的任何子视图。 这是非常有用的知识。 基本上,我发现根本不需要在tapThatEditButtonBaby中调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.