ios:uitableview部分标题锚到表顶部

我试图使多个部分(如联系应用程序)表一切顺利,我已经创build自定义部分标题显示代表此部分的字符的每个部分…问题是我希望它是像联系人确切地说,标题停留在顶部,直到下一个标题崩溃,这怎么可能发生

我正在使用下面的代码(为了让你更容易弄清楚

- (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; eg, 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; } 

这应该自动工作,如果您使用表视图样式“平原”而不是“分组”。

从文档:

UITableViewStylePlain:一个普通的表格视图。 任何节标题或页脚显示为行内分隔符,并在滚动表视图时浮动。

UITableViewStyleGrouped:一个表格视图,其部分呈现不同的行组。 部分页眉和页脚不会浮动。