iOS viewForHeaderInSection 覆盖其他单元格

iOS viewForHeaderInSection covering other cells

我正在尝试在我的应用程序中实现 header 视图,并使用以下代码创建 header 视图:

(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"FutureAppointmentsHeaderView" owner:self options:nil] lastObject];

    return view;
}

我还设置了 headerView 的高度如下:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 70;
}

当页面加载时,header 视图位于正确的位置(见第一张图片)。然而,当我滚动时 header 覆盖了其他单元格(见第二张图片。)

我不能因为我做错了什么而导致这种行为。非常感谢任何帮助。

这听起来不像是错误的行为。这就是 header 应该采取的行动。以 iPhone 上的通讯录应用程序为例 - 当您滚动时,带有当前字母的 header 总是会对齐到顶部。当您继续滚动时,它会被行中的下一个 header 替换。

您应该为 header 添加背景颜色,而不是让它保持透明。我认为一旦你这样做,一切都会看起来更好,更容易理解。

尝试更改您的 tableViewType。

UITableViewStyleGroup 可以让您 header 在滚动 tableView 时滚动

您正在为包含 - tableView:viewForHeaderInSection: 的部分添加 header。 header 部分位于 table 视图的顶部,并将覆盖其下方的单元格。如果您要向 table 视图添加另一个部分,每个部分将有两个相同的 header 视图,因为您返回视图时没有检查部分索引。

如果您想要浮动 header,请使用 tableHeaderView。 您可以在 viewDidLoad 中将视图分配给 UITableView 的 属性,或者通过将视图拖动到界面生成器中的 table 视图。参见 Table Header Views in StoryBoards

这是 tableview 的默认功能,不是错误。验证xib中headerview的背景颜色即可。它似乎有清晰的颜色,将其设置为任何其他颜色,您会发现不同之处。代码看起来不错。

看起来你header视图是透明的。您是否尝试过添加背景颜色?

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"FutureAppointmentsHeaderView" owner:self options:nil] lastObject];
view.backgroundColor = [UIColor whiteColor];