【问题标题】:UITableView - Table Header View leaves spacing if view is removedUITableView - 如果视图被删除,表头视图会留下间距
【发布时间】:2016-11-23 23:55:08
【问题描述】:

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

在删除表格视图标题时,我尝试了以下所有组合:

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

其他人遇到过这个吗?

表格视图标题代码:

- (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];
    }
}

【问题讨论】:

  • 工作正常,我只是试试!!你能解释一下吗?
  • @NSSakly 我根据需要添加了生成(和删除)表格视图标题的代码。问题是,如果我显示标题(当用户有警报时)然后他们查看它们,我想删除标题。但是当我删除它时,它会留下标题所在的空间(即表格顶部和第一部分/行之间的间隙)。

标签: ios uitableview spacing tableheader


【解决方案1】:

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

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

【讨论】:

  • 谢谢!你拯救了我的一天!
  • 你也拯救了我的一天 :)
  • Bug 在 iOS 11 中仍然存在。我认为唯一重要的是,高度不是 0。
  • 对于 iOS 13 及更高版本,这不是问题,但在较低版本中是。加上高度设置为 0 不会解决它,所以将其设为 1,如答案中给出的那样。
【解决方案2】:

请看你是否也在:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

【讨论】:

  • 如 OP 中所述,此问题与表视图标题有关,而不是节标题(即 self.tableView.tableHeaderView)。 heightForHeaderInSection 协议方法仅适用于节标题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多