Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: uitableview section header anchor to top of table

Tags:

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; } 
like image 436
Mohamed Emad Hegab Avatar asked Nov 06 '12 10:11

Mohamed Emad Hegab


1 Answers

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.

like image 122
Martin R Avatar answered Jan 13 '23 04:01

Martin R