ios 使用部分时是否可以有固定的 uitableview 标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23969587/
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
Is it possible to have a fixed uitableview Header while using sections?
提问by Houman
This question should not be mixed up with this here.. These are two different things.
There is a good examplehow to use a UITableView Header on SO.
有一个很好的例子如何在 SO 上使用 UITableView Header。
This all works fine and the main header is fixed on top as long as the style is set to plain
.
这一切正常,只要样式设置为 ,主标题就会固定在顶部plain
。
But if I use sections
, the main header no longer sticks to top and moves away while scrolling to the bottom.
但是如果我使用sections
,主标题不再粘在顶部并在滚动到底部时移开。
In this method, I am returning the header for each section.
在这种方法中,我返回每个部分的标题。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
In this method I am setting the height for the header section above:
在这种方法中,我设置了上面标题部分的高度:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
In this method, I am setting the real table header.
在这种方法中,我设置了真正的表头。
- (void)viewWillAppear:(BOOL)animated {
...
self.recordTableView.tableHeaderView = headerView;
}
Is it even possible having a fixed table header, while using sections? What is an alternative solution to this please?
在使用部分时甚至可以有一个固定的表头吗?请问这个问题的替代解决方案是什么?
采纳答案by Matteo Gobbi
There's no way to maintain the header of a tableView
fixed, but
an useful approach when you need a unique header, is to use a UIViewController
rather than a UITableViewController
, and set the header (UIView
) outfrom the tableView
.
有没有办法来维持的头tableView
固定的,但是当你需要一个独特的头一个有用的方法,是使用UIViewController
,而不是一个UITableViewController
,并设置标题(UIView
)出从tableView
。
Something like this:
像这样的东西:
回答by Joony
If you want a UITableViewController (static cells/keyboard handling) and have a fixed header then you should use Containment. You can do this from a Storyboard by setting up a UIViewController with your fixed header and then using a Container View to embed the UITableViewController.
如果你想要一个 UITableViewController(静态单元格/键盘处理)并有一个固定的标题,那么你应该使用 Containment。您可以通过使用固定标题设置 UIViewController,然后使用容器视图嵌入 UITableViewController 来从 Storyboard 中执行此操作。
Once you have your containing view setup, you right-click drag from the Container View to the View Controller you want to embed - the UITableViewController in this case.
设置包含视图后,右键单击从容器视图拖动到要嵌入的视图控制器 - 在本例中为 UITableViewController。
You can access and get a reference to the contained View Controller (the UITableViewController) from the Container View Controller by implementing the prepareForSegue:sender:
method.
您可以通过实现该prepareForSegue:sender:
方法从容器视图控制器访问并获取对包含的视图控制器(UITableViewController)的引用。
回答by amirfl
If you want to keep the class as a UITableViewController you can add your header as a subview to the tableview's superview. You will have to also push the tableview top inset down so your headerview doesnt hide the table.
如果您想将该类保留为 UITableViewController,您可以将标题作为子视图添加到 tableview 的 superview。您还必须将 tableview 顶部插入向下推,以便您的 headerview 不会隐藏表格。
Here is a sample code to put inside your tableViewController subclass (This example assumes your tableview controller is inside a navigation controller, so it pushes the view to below the navigation bar):
这是放置在 tableViewController 子类中的示例代码(此示例假设您的 tableview 控制器位于导航控制器内,因此它将视图推送到导航栏下方):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void)addHeaderView{
CGFloat yPosition = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
mainHeaderView = [[UIView alloc] init];
const CGFloat mainHeaderHeight = 44;
[mainHeaderView setFrame:CGRectMake(0, yPosition, self.view.frame.size.width, mainHeaderHeight)];
mainHeaderView.backgroundColor = [UIColor redColor];
[self.tableView.superview addSubview:mainHeaderView];
[self.tableView setContentInset:UIEdgeInsetsMake(yPosition + mainHeaderHeight, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right)];
}
回答by Dean Davids
I haven't done this, but the first thing I would think to try is to place my tableview in a UIView
and make my own header there in that UIView
. Seems a trivial matter to make that view appear to be the header of the table and it would certainly stay put.
我还没有这样做,但我想尝试的第一件事是将我的 tableview 放在 a 中,UIView
并在其中创建我自己的标题UIView
。使该视图看起来是表的标题似乎是一件小事,它肯定会保持原状。