ios 固定标题到 UITableview?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6951190/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:58:29  来源:igfitidea点击:

Fixed header to UITableview?

iosuitableview

提问by DOOManiac

I've got a UITableViewthat I'd like to stick a 44px subview on top of. I tried tableViewHeader, but that scrolls with the rest of the table.

我有一个UITableView我想在上面贴一个 44px 的子视图。我试过了tableViewHeader,但它会随着表格的其余部分滚动。

I tried searching and have found many people saying I need to add a UIView superview and then add my header and the UITableView to it. However I can't find an example on exactly how to do this. I tried making a new UIView subclass and laying out the subviews in IB, but I ran into trouble getting the table controller to link w/ the UITable (because I don't know enough about IB).

我尝试搜索,发现很多人说我需要添加一个 UIView 超级视图,然后将我的标题和 UITableView 添加到其中。但是,我找不到有关如何执行此操作的示例。我尝试创建一个新的 UIView 子类并在 IB 中布置子视图,但是我在让表控制器与 UITable 链接时遇到了麻烦(因为我对 IB 不够了解)。

How can I do this with XIBs? Can someone provide an example?

我怎样才能用 XIB做到这一点?有人可以提供一个例子吗?

Thanks for any help you guys can provide.

感谢你们提供的任何帮助。

回答by DOOManiac

I finally figured this out right after posting. Figures. :)

我终于在发布后立即想到了这一点。数字。:)

Here's what I did, in case others run into the same problem:

这是我所做的,以防其他人遇到同样的问题:

  1. Delete the existing UITableViewControllerand its XIB. They're junk. Get really mad while you do.

  2. Make a new UIViewControllersubclass with a XIB

  3. Open XIB in IB and add your header stuff and a UITableViewto the UIView

  4. In the IB Outlets for UITableViewmake sure you connect Delegate and DataSource to your File Owner

  5. In the header for your view controller, be sure to add <UITableViewDelegate, UITableViewDataSource>to implement these protocols

  6. Implement all the regular UITableViewdelegate and data source methods you know and love, but in your UIViewControllerinstead of the way you're used to doing it through UITableViewController

  1. 删除现有UITableViewController及其 XIB。他们是垃圾。当你这样做的时候真的很生气。

  2. UIViewController使用 XIB 创建一个新的子类

  3. 在 IB 中打开 XIB 并将您的标题内容和 a 添加UITableViewUIView

  4. 在 IB Outlets 中UITableView确保您将 Delegate 和 DataSource 连接到您的文件所有者

  5. 在您的视图控制器的标头中,确保添加 <UITableViewDelegate, UITableViewDataSource>以实现这些协议

  6. 实现所有UITableView您熟悉和喜爱的常规委托和数据源方法,但以您UIViewController习惯的方式实现UITableViewController

After this things should work.

在这之后事情应该工作。

回答by Steve Potter

The problem is, UITableViewController's view property is the same thing as the tableView property. I had the same problem, wanting to put some fixed content above the table. I didn't want to change the base class, as it provides lots of great functionality I didn't want to lose or disrupt.

问题是,UITableViewController 的 view 属性和 tableView 属性是一样的。我有同样的问题,想在桌子上方放一些固定的内容。我不想更改基类,因为它提供了许多我不想丢失或破坏的强大功能。

The fix is actually easy. The trick is to create custom set and get for self.tableView property. Then, in loadView, you replace the view with a fresh UIView and add the tableView to it. Then you're free to add subviews around the tableView. Here's how it's done:

修复实际上很容易。诀窍是创建自定义集并获取 self.tableView 属性。然后,在 loadView 中,用新的 UIView 替换视图并将 tableView 添加到其中。然后你可以自由地在 tableView 周围添加子视图。这是它的完成方式:

In header:

在标题中:

@interface CustomTableViewController : UITableViewController
{
    UITableView *tableView;
} 

In .m:

在.m:

- (UITableView*)tableView
{
    return tableView;
}

- (void)setTableView:(UITableView *)newTableView
{
    if ( newTableView != tableView )
    {
        [tableView release];
        tableView = [newTableView retain];
    }        
}

- (void)loadView {
    [super loadView];
    //save current tableview, then replace view with a regular uiview
    self.tableView = (UITableView*)self.view;
    UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
    self.view = replacementView;
    [replacementView release];
    [self.view addSubview:self.tableView];    

    //code below adds some custom stuff above the table
    UIView *customHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    customHeader.backgroundColor = [UIColor redColor];
    [self.view addSubview:customHeader];
    [customHeader release];

    self.tableView.frame = CGRectMake(0, customHeader.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - customHeader.frame.size.height);
}

Enjoy!

享受!

回答by vir us

As an option it is possible to embed UITableViewControlleras part of UI into another UIViewControllerwith 'Container View' element (pick one in Interface Builder from the Object library (where all other views are) ).

作为一种选择,可以将UITableViewController作为 UI 的一部分嵌入到另一个带有“ Container View”元素的UIViewController 中(从对象库中的 Interface Builder 中选择一个(所有其他视图都在那里))。

This way you can use UITableViewController like ordinary view (in terms of positioning) and compose any layout you need without overwritting existing table view code

这样你就可以像普通视图一样使用 UITableViewController(在定位方面),并在不覆盖现有表视图代码的情况下组合你需要的任何布局

EDIT:

编辑:

to further expand my answer, here are the steps to accomplish the described approach:

为了进一步扩展我的答案,以下是完成所描述方法的步骤:

  1. Open you storyboar in interface builder
  2. Drag'n'drop a new ViewControllerelement to the storyboard from Object Libraryto add a new controller.
  3. As a child view, drag'n'drop Container Viewfrom Object Library and place it anywhere inside the ViewController

  4. Container Viewcreates another view controller and "embedded" segue as a connection. It's save to delete this viewcontrollerand to connect the Container Viewwith the required view controller(as per the questions it's UITableViewController)

  5. To connect Container Viewwith UITableViewControllerjust select the container viewand control+drag to the UITableViewController - select "Embed" in the popup menu
  1. 在界面构建器中打开你的故事板
  2. 将新ViewController元素拖放到情节提要中Object Library以添加新控制器。
  3. 作为子视图,Container View从对象库中拖放并将其放置在ViewController

  4. Container View创建另一个视图控制器和“嵌入式”segue 作为连接。删除它viewcontroller并将其Container View与所需的连接view controller(根据UITableViewController的问题)保存

  5. 要连接Container ViewUITableViewController只需选择container view并控制+拖动到 UITableViewController - 在弹出菜单中选择“嵌入”

Now the controller will display inside the Container Viewwith respect to the container's position and boundaries.

现在控制器将显示在Container View容器的位置和边界的内部。

It's possible to get a link to the embeeded view controller - the system will fire "prepareForSegue" method of the host viewcontroller (created on the step 1 above) and the controller is in segue.destinationViewController property - one can customize it as required. Just make sure to set an identifier to the "embedded" segue in interface builder - this is the same process just like for any other segues.

可以获得嵌入视图控制器的链接 - 系统将触发主机视图控制器的“prepareForSegue”方法(在上面的步骤 1 中创建)并且控制器位于 segue.destinationViewController 属性中 - 可以根据需要对其进行自定义。只需确保在界面构建器中为“嵌入式”segue 设置标识符 - 这与任何其他 segue 的过程相同。

回答by iyepes

Replace the TableViewController with a ViewController, inside it add a UIView with fixed height to place the fixed content you need, and below that add a UITableView.

将 TableViewController 替换为 ViewController,在其中添加一个固定高度的 UIView 来放置您需要的固定内容,然后在其下方添加一个 UITableView。

Create an outlet to your TableView

为您的 TableView 创建一个插座

 @IBOutlet weak var tableView: UITableView!

You can reuse all the funcs you already have removing the override word for example

您可以重用您已经删除覆盖词的所有功能,例如

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let c = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell!
    c.textLabel?.text = "A title"
    c.detailTextLabel?.text = "A subtitle"
    return c
}

See this answer to add the automatic refresh control if you need it Pull to refresh UITableView without UITableViewController

如果需要,请参阅此答案以添加自动刷新控件,无需UITableViewController 即可使用 Pull 刷新 UITableView

回答by Bharath Vankireddy

Define a custom UIView in storyboard or xib, have a IBOutlet reference for that UIView in View Controller. In -(void)viewWillAppear:(BOOL)animatedmethod write [self.tableView.superview addSubview:filterHeaderView];, here filterHeaderViewis the IBOutlet reference for my header view which I want to add as fixed header in my tableview.

在故事板或 xib 中定义自定义 UIView,在视图控制器中为该 UIView 提供 IBOutlet 参考。在-(void)viewWillAppear:(BOOL)animated方法 write 中[self.tableView.superview addSubview:filterHeaderView];,这里filterHeaderView是我的标题视图的 IBOutlet 参考,我想将其添加为我的表格视图中的固定标题。