大家好, 我读到UITableView的默认行为是在您滚动各节时将各节标题行固定在表的顶部,直到下一节将前一节行移出视图.

我在一辆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;
}

推荐答案

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

Ios相关问答推荐

SWIFT图表轴订购问题

如果条件不满足,S从@ViewBuilder返回什么?

在SwiftUI中动态隐藏列表的空部分

如何在本地iOS没有插件的情况下处理平台线程?

如何使用Swift以编程方式更改SVG文件中某些元素的 colored颜色

为什么IOS多行文本输入在React原生纸张对话框中无法正常工作?

Apple Metal iOS 为什么 GLKMatrix4MakeLookAt 来回摇摆

react 本机 |错误:无法解析 node_modules/react-native/index.js 中的模块

CarPlay:现在播放alert 可能吗?

CGPath intersection(_:using:) 和朋友(iOS 16 中的新功能)坏了?

我正在try 制作一种 Select 器样式,例如设置中的外观 Select 器

在视图控制器中实例化视图控制器是否可能/智能?

如何包含 Xcode 子项目标头?

如何在不支持并发的自动关闭中修复'async'调用?

使用 @MainActor 更新 UI 的适当策略是什么?

在 UICollectionView 上动态设置布局导致莫名其妙的 contentOffset 变化

setNeedsLayout 和 setNeedsDisplay

如何用 CAShapeLayer 和 UIBezierPath 画一个光滑的圆?

是否可以将 Socket.io 与 AWS Lambda 一起使用?

如何在 Swift 中更改按钮标题对齐方式?