带有固定节头的 UITableView

你好, 我读到,UITableView的默认行为是在滚动节时将节标题行固定在表的顶部,直到下一节将前一节行推出视图。

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

这只是 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;
}
129948 次浏览

The headers only remain fixed (floating) 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 (will not float).

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 样式:

Tableview = [[ UITableView alloc ] initwithFrame: frame 样式: UITableViewStyleGrouping ] ;

根据 UITableView 的苹果文档:

UITableViewStylePlain- A plain table view. Any section headers or 页脚显示为内联分隔符,当表 视图滚动。

UITableViewStyleGroupe-一个表格视图,其中的部分显示不同的 行组。节标题和页脚不浮动。

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

Swift 3.0

使用 UITableViewDelegateUITableViewDataSource协议创建一个 ViewController。然后在其中创建一个 tableView,将其样式声明为 分组。这会修正标题。

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

to make UITableView sections header not sticky or sticky:

  1. 改变表格视图的风格-使其分组为不粘性,并使其明确的粘性部分标题-不要忘记: 你可以做到这一点从故事板没有编写代码。(点击你的表格视图,从右侧/组件菜单改变它的样式)

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