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;
}
Mohamed Emad Hegab
  • 2,665
  • 6
  • 39
  • 64

1 Answers1

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 5
    Don't forget to use - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; if you want different height for each header. – NadavN7 Nov 06 '12 at 11:05
  • 2
    oh my this like a perfect answer and perfect comment...bless you both – Mohamed Emad Hegab Nov 06 '12 at 11:34
  • 2
    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. – LyricalPanda Mar 13 '13 at 20:54
  • @LyricalPanda: I don't know. – Martin R Mar 13 '13 at 21:24