简体   繁体   中英

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

I would like to implement segmented controls like this (circled with red) :

在此处输入图片说明

(When we scroll this view, the segmented controls stay sticky at the top of the view, that makes me think it's a cell on its own ? but I may be wrong).

I have already implemented a custom cell (that displays kind of what is above the red ellipse in the above picture, in purple) and add it to the header of my table view like so :

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

Now, I think I should create a second cell with the segmented controls in it and add it also to the table header (and not in the section header, because I have many sections with their titles).

Then, I would create a UIView containing this two cells and add this view as the header of my tableView ? Is this a correct way to do it ? Thank you very much for the help !

I think you have a couple of options.

1) Make a container view to host both of your "cells" (which needn't be UITableViewCells - just views...). Add the singular container view as the table header.

2) Forego using the table header altogether and just place your views above the table, making it shorter. This is more complicated if you're using a UITableViewController, but simple if you're just hosting a UITableView in some other custom UIViewController.

this is an example with uilabel that stick to the top - just change it to your 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM