ios 带有固定部分标题的 UITableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17582818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:19:20  来源:igfitidea点击:

UITableView with fixed section headers

iosobjective-cuitableview

提问by topwik

Greets, I'm reading that the default behaviour of UITableViewis 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.

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

I have a UITableViewinside a UIViewControllerand this does not seem to be the case.

我有一个UITableViewinside a UIViewController,但情况似乎并非如此。

Is that just the defualt behaviour for UITableViewController?

这只是默认行为UITableViewController吗?

Here's some simplified code based on what I have. I'll show the UIControllerinterface 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.

这是基于我所拥有的一些简化代码。我将展示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;
}

回答by bachonk

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

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

回答by Aks

Change your TableView Style:

更改您的 TableView 样式:

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

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

As per apple documentation for UITableView:

根据 UITableView 的苹果文档:

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

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

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

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

Hope this small change will help you ..

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

回答by Nursultan Askarbekuly

Swift 3.0

斯威夫特 3.0

Create a ViewControllerwith the UITableViewDelegateand UITableViewDataSourceprotocols. Then create a tableView inside it, declaring its style to be UITableViewStyle.grouped. This will fix the headers.

使用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
}()

回答by kevinl

You can also set the tableview's bounces property to NO. This will keep the section headers non-floating/static, but then you also lose the bounce property of the tableview.

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

回答by Burcu Kutluay

to make UITableView sections header not sticky or sticky:

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

  1. change the table view's style - make it grouped for not sticky & make it plain for sticky section headers - do not forget: you can do it from storyboard without writing code. (click on your table view and change it is style from the right Side/ component menu)

  2. if you have extra components such as custom views or etc. please check the table view's margins to create appropriate design. (such as height of header for sections & height of cell at index path, sections)

  1. 更改表格视图的样式 - 将其分组为不粘,并为粘性部分标题使其简单 - 不要忘记:您可以在不编写代码的情况下从故事板中进行。(单击您的表格视图并从右侧/组件菜单更改其样式)

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

回答by Ravi Kumar

Now your tableview look like plain table style but don't float buz setting table style set to group.

现在您的 tableview 看起来像普通的 table 样式,但不要浮动 buz 设置 table 样式设置为 group。

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