25

我正在尝试制作一个包含多个部分(如联系人应用程序)的表格,一切顺利,我在每个部分的顶部创建了自定义部分标题,显示代表该部分的字符..问题是我希望它像联系人正是标题停留在顶部的位置,直到下一个标题随之崩溃……这怎么可能发生

我正在使用以下代码(让您更容易弄清楚

    - (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;
}
4

1 回答 1

44

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

从文档中:

UITableViewStylePlain:一个普通的表格视图。滚动表格视图时,任何部分的页眉或页脚都显示为内联分隔符并浮动。

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

于 2012-11-06T10:58:18.547 回答