ios - UITableView - 如果 View 被移除,表头 View 会留下间距

标签 ios uitableview spacing tableheader

我有一个 UITableViewController,如果满足某些条件,它将显示 TableView 标题(不是部分标题)。在加载时显示带或不带标题 View 的 Controller 都很好。但是,如果我显示标题,然后将其删除,间距(标题框架所在的位置)每次都会保留在表格 View 的顶部。

在没有运气的情况下删除 TableView 标题时,我尝试了以下所有组合:

[self.tableView beginUpdates];
self.tableView.tableHeaderView = nil;
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
[self.tableView reloadData];
[self.tableView endUpdates];

还有其他人遇到过这个吗?

TableView 标题代码:

- (void)updateTableHeaderView
{
    if (self.alerts.count && self.isViewLoaded) {
        [self.tableView beginUpdates];

        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), 40.0f)];
        headerView.preservesSuperviewLayoutMargins = YES;
        self.tableView.tableHeaderView = headerView;

        UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeCustom];
        alertButton.tintColor = [UIColor whiteColor];
        alertButton.translatesAutoresizingMaskIntoConstraints = NO;
        [alertButton setBackgroundImage:[UIImage imageWithColor:alertTintColor] forState:UIControlStateNormal];
        [alertButton setBackgroundImage:[UIImage imageWithColor:alertHighlightedTintColor] forState:UIControlStateHighlighted];
        [alertButton addTarget:self action:@selector(alertButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:alertButton];

        NSDictionary *views = NSDictionaryOfVariableBindings(alertButton);
        NSDictionary *metrics = nil;

        [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[alertButton]|" options:0 metrics:metrics views:views]];
        [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[alertButton]|" options:0 metrics:metrics views:views]];

        [self.tableView endUpdates];
    }
    else if (self.isViewLoaded) {
        [self.tableView beginUpdates];
        self.tableView.tableHeaderView = nil;
        [self.tableView endUpdates];
    }
}

最佳答案

这似乎是一个 iOS 错误,这是删除 tableHeaderView 的当前解决方法,它的间距直到固定:

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView.bounds), 1.0f)];

关于ios - UITableView - 如果 View 被移除,表头 View 会留下间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38486451/

相关文章:

CSS,连续图片库,每张图片之间的空间,

带有 RMStore 的 iOS SDK Auto Renewable 订阅 - 如何验证有效订阅?

ios - 根据 TableView 部分和行重新排序(重新定位)数据模型

iOS 8.4 UITabelview scrollToRowAtIndexPath 在设备中不起作用

ios - viewWithTag:reloadData 后返回 nil

java - 初学者java - 向后打印直角三角形

android - 渲染 BIM 模型

ios - 带有不同颜色的 UIBarButtonItem 的 UIToolBar

ios - uitableview更新错误

html - @media 打印边距/padding-top 不工作