2013-07-10 30 views
78

问候, 我在读,UITableView的默认行为是当您滚动浏览部分时将部分标题行固定到表格顶部,直到下一部分将previos部分行从视图。带固定节标题的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; 
} 
+0

这应该有效。显示一些代码。 –

+0

这是使用UITableView的完美方式,并且应该使用标题。你的情况是不是头文件? – Eric

+0

@Eric头部正在滚动行。当我滚动时,他们不停在桌子的顶部。我已经添加了我用来生成我的表格视图的代码。这是UIViewController中的UITableView。 – topwik

回答

233

的头时,在表的UITableViewStyle属性设置为UITableViewStylePlain仅保持固定。如果您将它设置为,则标题将向上滚动单元格。

+0

我的tableview风格是UITableViewStylePlain,但它也向上滚动单元格。 – yudun1989

+6

对此有几种可能的解释。一个重要的注意事项是,标题只会保留其部分内的单元格。所以,如果你在第0节有一个头文件,它将不会在第1节的单元格中保持不变。另一个可能的解释是你没有用正确的样式初始化你的UITableView。建议您使用'initWithStyle:UITableViewStylePlain',因为像tableView.style = UITableViewStylePlain这样的调用将不起作用。 – bachonk

+0

UITableViewStyleGrouped使页眉/页脚与其他单元格固定,而UITableViewStylePlain在页眉/页脚到达tableview的顶部/底部时使页眉/页脚“浮动”。 – StoneLam

1

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

+0

你救了我的命!感谢Kevinl! – BurtK

0

更改的TableView风格:

self.tableview = [[UITableView的页头] initWithFrame:方法帧 样式:UITableViewStyleGrouped];

作为每对的UITableView苹果文档:

UITableViewStylePlain-的滑动表视图。任何节标题或 页脚都显示为内联分隔符,并在滚动表 视图时显示为浮点。

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

希望这个小变化将帮助你..

+1

这看起来与问题所要求的完全相反 - 它应该是'Plain',而不是'Grouped' – CupawnTae

1

雨燕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 
}() 
0

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

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