带有固定节标题的UITableView

时间:2013-07-10 23:46:33

标签: ios objective-c uitableview

电贺, 我正在读到UITableView的默认行为是当您滚动这些部分直到下一部分将previos部分行推出视图时将部分标题行固定到表的顶部。

我在UITableView内有一个UIViewController,但情况似乎并非如此。

这只是UITableViewController的默认行为吗?

这是基于我所拥有的一些简化代码。 我将展示UIController接口和我实现的每个表视图方法来创建表视图。 我有一个帮助器数据源类,可以帮助我索引我的对象以便与表一起使用。

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

6 个答案:

答案 0 :(得分:272)

当表的UITableViewStyle属性设置为UITableViewStylePlain时,标头仅保持固定状态。如果您将其设置为UITableViewStyleGrouped,标题将向上滚动单元格。

答案 1 :(得分:3)

您还可以将tableview的跳出属性设置为NO。这将使节标题保持非浮动/静态,但您也会丢失tableview的bounce属性。

答案 2 :(得分:2)

更改TableView样式:

  

self.tableview = [[UITableView alloc] initwithFrame:frame   式:UITableViewStyleGrouped];

根据UITableView的Apple文档:

  

UITableViewStylePlain-普通表视图。任何部分标题或   页脚显示为内联分隔符并在表格中浮动   视图滚动。

     

UITableViewStyleGrouped-一个表格视图,其截面显示不同   行组。部分页眉和页脚不会浮动。

希望这个小改变能帮到你..

答案 3 :(得分:2)

Swift 3.0

使用 UITableViewDelegate UITableViewDataSource 协议创建 ViewController 。然后在其中创建一个tableView,声明其样式为 UITableViewStyle.grouped 。这将修复标题。

lazy var tableView: UITableView = {
    let view = UITableView(frame: UIScreen.main.bounds, style: UITableViewStyle.grouped)
    view.delegate = self
    view.dataSource = self
    view.separatorStyle = .none
    return view
}()

答案 4 :(得分:0)

  

现在你的tableview看起来像普通的表样式,但不要浮动buz   将表格样式设置为分组。

[_tableView setBackgroundView:nil];
_tableView.backgroundColor = [UIColor whiteColor];

答案 5 :(得分:0)

使UITableView节标题不粘或粘:

  1. 更改表格视图的样式 - 将其分组为非粘性&amp;为粘性部分标题明确表示 - 不要忘记:你可以在没有编写代码的情况下从故事板中完成它。 (单击您的表格视图并从右侧/组件菜单更改它的样式)

  2. 如果您有额外的组件,例如自定义视图等,请检查表格视图的边距以创建合适的设计。 (例如节的标题高度和索引路径上的单元格高度,部分)

相关问题