如何在 table 视图(不是 header 部分)的 header 中添加多个单元格?

How to add multiple cells in header of a table view (not section header)?

我想实现这样的分段控件(红圈):

(当我们滚动这个视图时,分段控件会粘在视图的顶部,这让我觉得它是一个独立的单元格?但我可能错了)。

我已经实现了一个自定义单元格(以紫色显示上图中红色椭圆上方的内容)并将其添加到我的 table 视图的 header 中,例如所以 :

BigCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"BigCell"];
self.tableView.tableHeaderView = cell;

现在,我想我应该创建第二个包含分段控件的单元格,并将其也添加到 table header(而不是 header 部分,因为我有很多带有标题的部分)。

然后,我将创建一个包含这两个单元格的 UIView 并将此视图添加为我的 tableView 的 header?这是正确的方法吗? 非常感谢您的帮助!

这是一个 uilabel 贴在顶部的示例 - 只需将其更改为您的 uisegmentedcontrol

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *viewForSectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, SETTINGS_HEADER_HEIGHT)];
    [viewForSectionHeader setBackgroundColor:[Utils colorHeaderBlue]];
    UILabel *lblSectionTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, SETTINGS_HEADER_HEIGHT)];
    lblSectionTitle.text = @"PROFILE";
    lblSectionTitle.textAlignment = NSTextAlignmentCenter;
    lblSectionTitle.textColor = [UIColor whiteColor];
    [viewForSectionHeader addSubview:lblSectionTitle];

    return viewForSectionHeader;
}

我想你有几个选择。

1) 创建一个容器视图来承载您的两个 "cells"(不必是 UITableViewCells - 只是视图...)。添加单一容器视图作为 table header.

2) 完全放弃使用 table header,将您的视图放在 table 之上,使其更短。如果您使用的是 UITableViewController,这会更复杂,但如果您只是在其他一些自定义 UIViewController 中托管 UITableView,则很简单。