1

我有一个带有 a 的自定义视图UILabelUIButton我将其用于分组的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.
    }
}

解决这个问题的好方法是什么?理想情况下,在我看来,我希望能够调用诸如 updateSectionAndFooterAtIndex: from 之类的东西UITableView,但是没有这种公开的方法... :(

4

2 回答 2

2

更新

很抱歉上一个答案,我没有注意到你有任何部分的标题。因此,针对您的情况的解决方案几乎相同。您可以将带有部分索引的 NSDictionary 存储为您感兴趣的键和按钮以获得价值。这本字典将填写在你 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 这样的:

- (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)在您的情况下,如果您只想根据编辑状态更改按钮,您可以为您的按钮创建属性并在需要时更改它。

于 2014-07-01T08:52:37.803 回答
-1

实际上,我发现我的问题的奥秘在于,如果直接对视图属性进行了对节标题视图的视觉更改,则它们不会得到更新。

如果我添加这个方法 setEditing:animated: 在我的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 中调用它。

于 2014-07-01T09:28:35.630 回答