【问题标题】:UITableView with fixed section headers带有固定部分标题的 UITableView
【发布时间】:2013-07-09 02:26:20
【问题描述】:

您好, 我读到UITableView 的默认行为是将部分标题行固定到表格顶部,因为您滚动浏览各个部分,直到下一个部分将 previos 部分行推到视野之外。

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

这只是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;
}

【问题讨论】:

  • 应该可以。显示一些代码。
  • 这是使用 UITableView 的完全好方法,并且应该与标题一起使用。在您的情况下,标题没有做什么?
  • @Eric 标题随着行滚动。当我滚动时,它们并没有停在桌子的顶部。我添加了用于生成表格视图的代码。它是 UIViewController 中的 UITableView。

标签: ios objective-c uitableview


【解决方案1】:

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

【讨论】:

  • 我的 tableview 样式是 UITableViewStylePlain,但它也随着单元格向上滚动。
  • 对此有几种可能的解释。一个重要的注意事项是,标题只会在其部分内的单元格中保持固定。因此,如果您在第 0 节中有一个标题,那么第 1 节中的单元格将不会保持固定。另一个可能的解释是您没有使用正确的样式初始化 UITableView。建议使用initWithStyle:UITableViewStylePlain,因为调用 tableView.style = UITableViewStylePlain 将不起作用。
  • UITableViewStyleGrouped 使页眉/页脚与其他单元格固定,而 UITableViewStylePlain 使页眉/页脚在页眉/页脚到达 tableview 的顶部/底部时“浮动”。
  • @bachonk 我正在使用可拉伸的标题视图。我的风格是 UITableViewStyleGrouped。我想在向上滚动时修复标题。如果我将它设为 UITableViewStylePlain,那么标题将固定在屏幕中间,我想向下滚动标题,但是向上滚动时我希望它固定在第一行.. 有可能吗?
【解决方案2】:

更改您的 TableView 样式:

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

根据苹果的 UITableView 文档:

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

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

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

【讨论】:

  • 这似乎与所要求的问题完全相反 - 它应该是 Plain,而不是 Grouped
【解决方案3】:

Swift 3.0

使用 UITableViewDelegateUITableViewDataSource 协议创建一个 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】:

    您也可以将tableview 的bounces 属性设置为NO。这将使节标题保持非浮动/静态,但随后您也会失去 tableview 的反弹属性。

    【讨论】:

      【解决方案5】:

      使 UITableView 部分标题不粘或不粘:

      1. 更改表格视图的样式 - 将其分组为不粘,并将其明确用于粘性部分标题 - 不要忘记:您可以在情节提要中完成此操作而无需编写代码。 (单击您的表格视图并从右侧/组件菜单中更改其样式)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-21
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多