带有固定部分标题的 UITableView [英] UITableView with fixed section headers

查看:27
本文介绍了带有固定部分标题的 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我读到 UITableView 的默认行为是在您滚动浏览各部分时将部分标题行固定到表格顶部,直到下一部分将前一个部分行推出视图.>

我在 UIViewController 中有一个 UITableView 而这似乎不是这种情况.

这只是 UITableViewController 的默认行为吗?

这里有一些基于我所拥有的简化代码.我将展示 UIController 界面和我为创建表格视图而实现的每个表格视图方法.我有一个辅助数据源类,可以帮助我索引我的对象以与表一起使用.

 @interface MyUIViewController()@property (nonatomic, readonly) UITableView *myTableView;@property (nonatomic, readonly) MyCustomHelperDataSource *helperDataSource;@结尾//设置节数据后,获取每个节的详细信息并在成功时重新加载表- (void)setSectionData:(NSArray *)sections {super.sectionData = 部分;//这个数组驱动节//获取部分详细信息的附加数据[[RestKitService sharedClient] getSectionDetailsForSection:someId成功:^(RKObjectRequestOperation *操作,RKMappingResult *详细信息){NSLog(@"得到了部分详细数据");_helperDataSource = [[MyCustomHelperDataSource alloc] initWithSections:sections andDetails:details.array];[myTableView reloadData];} 失败:^(RKObjectRequestOperation *operation, NSError *error) {NSLog(@"获取章节详细信息失败");}];}#pragma mark - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {如果(!_helperDataSource)返回0;返回 [_helperDataSource countSectionsWithDetails];//有详细信息行的节数,忽略任何空节}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {//获取当前节的节对象 intSectionObject *section = [_helperDataSource sectionObjectForSection:section];//返回此部分的部分对象的详细信息行数返回 [_helperDataSource countOfSectionDetails:section.sectionId];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell * 单元格;NSString *CellIdentifier = @"SectionDetailCell";cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(单元格 == 零){initWithStyle:UITableViewCellStyleValue1 重用标识符:CellIdentifier];cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle ReuseIdentifier:CellIdentifier];cell.textLabel.font = [UIFont systemFontOfSize:12.0f];}//获取此部分的详细信息对象SectionObject *section = [_helperDataSource sectionObjectForSection:indexPath.section];NSArray* detailsForSection = [_helperDataSource detailsForSection:section.sectionId] ;SectionDetail *sd = (SectionDetail*)[detailsForSection objectAtIndex:indexPath.row];cell.textLabel.text = sd.displayText;cell.detailTextLabel.text = sd.subText;cell.detailTextLabel.textColor = [UIColor blueTextColor];返回单元格;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {返回 50.0f;}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {返回 30.0f;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) 部分{//获取当前节的节对象SectionObject *section = [_helperDataSource sectionObjectForSection:section];NSString *title = @"%@ (%d)";返回 [NSString stringWithFormat:title, section.name, [_helperDataSource countOfSectionDetails:section.sectionId]];}- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 0)];header.autoresizingMask = UIViewAutoresizingFlexibleWidth;header.backgroundColor = [UIColor darkBackgroundColor];SSLabel *label = [[SSLabel alloc] initWithFrame:CGRectMake(3, 3, 260, 24)];label.font = [UIFont boldSystemFontOfSize:10.0f];label.verticalTextAlignment = SSLabelVerticalTextAlignmentMiddle;label.backgroundColor = [UIColor clearColor];label.text = [self tableView:tableView titleForHeaderInSection:section];label.textColor = [UIColor whiteColor];label.shadowColor = [UIColor darkGrayColor];label.shadowOffset = CGSizeMake(1.0, 1.0);[header addSubview:label];返回标题;}

解决方案

只有当表的 UITableViewStyle 属性设置为 UITableViewStylePlain 时,标题才保持固定.如果您将其设置为 UITableViewStyleGrouped,标题将与单元格一起向上滚动.

Greets, I'm reading that the default behaviour of UITableView is to pin section header rows to the top of the table as you scroll through the sections until the next section pushes the previos section row out of view.

I have a UITableView inside a UIViewController and this does not seem to be the case.

Is that just the defualt behaviour for UITableViewController?

Here's some simplified code based on what I have. I'll show the UIController interface and each table view method I've implemented to create the table view. I have a helper data source class that helps me index my objects for use with the table.

    @interface MyUIViewController ()<UITableViewDelegate, UITableViewDataSource>
        @property (nonatomic, readonly) UITableView *myTableView;
        @property (nonatomic, readonly) MyCustomHelperDataSource *helperDataSource;
    @end

    //when section data is set, get details for each section and reload table on success
    - (void)setSectionData:(NSArray *)sections {
        super.sectionData = sections; //this array drives the sections

        //get additional data for section details
        [[RestKitService sharedClient] getSectionDetailsForSection:someId 
        success:^(RKObjectRequestOperation *operation, RKMappingResult *details) {
            NSLog(@"Got section details data");
            _helperDataSource = [[MyCustomHelperDataSource alloc] initWithSections:sections andDetails:details.array];
            [myTableView reloadData];
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"Failed getting section details");
        }];
    }

    #pragma mark <UITableViewDataSource, UITableViewDelegate>

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (!_helperDataSource) return 0;
        return [_helperDataSource countSectionsWithDetails]; //number of section that have details rows, ignore any empty sections
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //get the section object for the current section int
        SectionObject *section = [_helperDataSource sectionObjectForSection:section];
        //return the number of details rows for the section object at this section
        return [_helperDataSource countOfSectionDetails:section.sectionId];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell * cell;

        NSString *CellIdentifier = @"SectionDetailCell";

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            cell.textLabel.font = [UIFont systemFontOfSize:12.0f];
        }

        //get the detail object for this section
        SectionObject *section = [_helperDataSource sectionObjectForSection:indexPath.section]; 

        NSArray* detailsForSection = [_helperDataSource detailsForSection:section.sectionId] ;
        SectionDetail *sd = (SectionDetail*)[detailsForSection objectAtIndex:indexPath.row];

        cell.textLabel.text = sd.displayText;
        cell.detailTextLabel.text = sd.subText;
        cell.detailTextLabel.textColor = [UIColor blueTextColor];

        return cell;
    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30.0f;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {
    //get the section object for the current section
    SectionObject *section = [_helperDataSource sectionObjectForSection:section]; 

    NSString *title = @"%@ (%d)";

    return [NSString stringWithFormat:title, section.name, [_helperDataSource countOfSectionDetails:section.sectionId]];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 0)];
    header.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    header.backgroundColor = [UIColor darkBackgroundColor];

    SSLabel *label = [[SSLabel alloc] initWithFrame:CGRectMake(3, 3, 260, 24)];
    label.font = [UIFont boldSystemFontOfSize:10.0f];
    label.verticalTextAlignment = SSLabelVerticalTextAlignmentMiddle;
    label.backgroundColor = [UIColor clearColor];
    label.text = [self tableView:tableView titleForHeaderInSection:section];
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor darkGrayColor];
    label.shadowOffset = CGSizeMake(1.0, 1.0);
    [header addSubview:label];

    return header;
}

解决方案

The headers only remain fixed when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.

这篇关于带有固定部分标题的 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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