ios: uitableview section header anchor to top of table
Asked Answered
I

1

25

i'm trying to make a table with multiple section (like contact app) everything went well and i've created custom section header on top of each section displaying the character representing this section..the problem is i want it to be like Contact exactly where the header stay on the top till the next header collapse with it ...how can this happen

i'm using the following code (to make it easier for you to figure out

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
   return [stateIndex count];
}



- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section
{

    UILabel *sectionHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    sectionHeader.backgroundColor = [UIColor clearColor];
    sectionHeader.font = [UIFont boldSystemFontOfSize:18];
    sectionHeader.textColor = [UIColor whiteColor];
    sectionHeader.text = [stateIndex objectAtIndex:section];
    sectionHeader.textAlignment=UITextAlignmentCenter;
    return sectionHeader;
}
//---set the index for the table---
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return stateIndex;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //---get the letter in each section; e.g., A, B, C, etc.---
    NSString *alphabet = [stateIndex objectAtIndex:section];

    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *states = [resultPoets filteredArrayUsingPredicate:predicate];

    //---return the number of states beginning with the letter---
    return [states count];

}


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    PoemsCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    int row = indexPath.row;




    //---get the letter in the current section---
    NSString *alphabet = [stateIndex objectAtIndex:[indexPath section]];

    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *states = [resultPoets filteredArrayUsingPredicate:predicate];

    if ([states count]>0) {
        //---extract the relevant state from the states object---
        NSString *cellValue = [states objectAtIndex:row];

        cell.poemText.text = cellValue;

    }



    return cell;
}
Impartial answered 6/11, 2012 at 10:41 Comment(0)
C
44

This should work automatically if you use the Table View Style "Plain" and not "Grouped".

From the documentation:

UITableViewStylePlain: A plain table view. Any section headers or footers are displayed as inline separators and float when the table view is scrolled.

UITableViewStyleGrouped: A table view whose sections present distinct groups of rows. The section headers and footers do not float.

Cleptomania answered 6/11, 2012 at 10:58 Comment(4)
Don't forget to use - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; if you want different height for each header.Shastashastra
oh my this like a perfect answer and perfect comment...bless you bothImpartial
Is it possible to modify 'Plain' to show the header section, even if I've scrolled into a different section? I'd like for the headers to be aggregated into the top, since I can collapse/expand them. This just helps navigate between them if the table is very large.Sweetener
@LyricalPanda: I don't know.Cleptomania

© 2022 - 2024 — McMap. All rights reserved.