ios - 当表格处于编辑模式时,在 UITableView 部分标题上切换 UIButton

标签 ios objective-c uitableview custom-view sectionheader

我有一个带有 UILabelUIButton 的自定义 View ,我将其用于分组的 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 的情况下刷新可见节标题 View 。该方法基本上返回一个 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: 的方法,但是没有这样公开的方法...:(

最佳答案

更新

很抱歉上一个回答,我没有注意到你有任何部分的标题。 因此,针对您的情况的解决方案几乎相同。您可以只存储 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) 创建单独的方法来创建页眉/页脚 View ,例如:

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

关于ios - 当表格处于编辑模式时,在 UITableView 部分标题上切换 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24505630/

相关文章:

iphone - TableView 中的自定义部分

ios - 当任何其他 UIViewController 被插入堆栈时如何从 UITabBarController 隐藏 TabBar

ios - 如何计算 UIScrollView 结束拖动所需的时间?

ios - 根据ios中的未读消息更新应用程序图标的角标(Badge)计数

iphone - 如何隐藏UITableView中的行分隔符而不隐藏组边框?

ios - 我们可以将 NSURLRequest 直接存储到 Core Data 中吗?

具有自定义对象的 Android 自定义 ArrayAdapter

ios - 如何保存选定的 TableView 行数据并在用户返回屏幕时显示?

ios - 我如何在 Objective-C 中的 WKWebView 的 facebook 嵌入式视频中设置 "playsinline=1"

ios - Swift - UIButton 按下时未突出显示