ios:uitableview节头锚到表顶部 [英] ios: uitableview section header anchor to top of table

查看:96
本文介绍了ios:uitableview节头锚到表顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

如果您自动生效使用表视图样式普通而不是分组。

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

来自文档:


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

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

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

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

这篇关于ios:uitableview节头锚到表顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆