使用Storyboard时如何使UITable标题贴在屏幕顶部?

Ego*_*gor 7 xcode storyboard uitableview ios

我使用这篇文章制作了标题:StoryBoards中的表头视图

但是我无法在滚动时将标题粘贴(固定)到屏幕顶部.

怎么能实现呢?

PS表风格很简单,当我厌倦了超载viewForHeaderInSection并返回标题视图的插座时,它变得更糟.

提前致谢!

A-L*_*ive 8

这是我认为使标题视图坚持到桌面的代码(这不是它的默认行为):

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect rect = self.tableHeaderView.frame;
    rect.origin.y = MIN(0, self.tableView.contentOffset.y);
    self.tableHeaderView.frame = rect;
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是为第一部分提供标题视图.你不能使用任何子视图viewForHeaderInSection,你要回到那里是没有在视图层次中的视图.该方法的优点是节标题视图设计在桌面上,缺点是您可能希望在特征中具有节的标题,它将破坏解决方案.


Ego*_*gor 2

我能够做到这一点(至少在 iOS 6 中),不知道为什么这个东西有效:)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if(self.headerManualView == nil) {
        //allocate the view if it doesn't exist yet
        headerManualView  = [[UIView alloc] init];

        //create the button
        UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 250, 44)];

        //the button should be as big as a table view cell
        txtField.borderStyle = UITextBorderStyleRoundedRect;

        //set action of the button
        //[txtField addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchUpInside];

        //add the button to the view
        [headerManualView addSubview:txtField];
    }

    //return the view for the footer
    return headerManualView;
}
Run Code Online (Sandbox Code Playgroud)