【问题标题】:toggle a UIButton on the UITableView section header when table in edit mode当表格处于编辑模式时,在 UITableView 部分标题上切换 UIButton
【发布时间】:2014-07-01 08:25:33
【问题描述】:

我有一个带有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.
    }
}

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

【问题讨论】:

    标签: ios objective-c uitableview custom-view sectionheader


    【解决方案1】:

    更新

    抱歉上一个答案,我没有注意到你有任何部分的标题。 因此,针对您的情况的解决方案几乎相同。您可以将带有部分索引的 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) 在您的情况下,如果您只想根据编辑状态更改按钮,您可以为您的按钮创建属性并在需要时更改它。

    【讨论】:

    • 对不起,我自己解决了,但感谢您的努力!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2021-11-12
      • 2011-01-19
      • 1970-01-01
      • 2010-10-09
      • 2012-04-13
      相关资源
      最近更新 更多