在Swift中的UITableViewController顶部添加一个UIView [英] Add a UIView over the top of UITableViewController in Swift

查看:108
本文介绍了在Swift中的UITableViewController顶部添加一个UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是UITableViewController(PFQueryTableViewController),我想在TableView的顶部显示一个UIView。



理想情况下,我想在故事板,所以我可以轻松地添加额外的标签/按钮。但是,我似乎无法弄清楚如何或如果你能在故事板中做到这一点。是吗?



我已经以编程方式尝试过。我首先在顶部创建变量:



var filterLabelView:UIView = UIView()



然后我在 ViewDidLoad 中添加了以下内容:

  filterLabelView.frame = CGRect(x:self.view.frame.width / 2,y:self.view.frame.height / 2,width:self.view.frame.width,height:40)
filterLabelView.center = CGPointMake(self.view.frame.size.width / 2,self.view.frame.size.height / 2)
filterLabelView.backgroundColor = UIColor.redColor()
self.view.addSubview(filterLabelView)//见下面

我也尝试过:

  self.view.insertSubview(filterLabelView,aboveSubview:tableView)

这会创建红色UIView,但它似乎嵌入到tableview中,因为当我向上和向下滚动时,View会随之移动。



我想创造什么



理想情况下,我希望视图能够坐下来在屏幕底部,当用户滚动时移动。我希望它看起来的示例截图如下:





我已经读过,最好的方法是在其中使用带有UITableView的UIViewController,但我不想采用这种方法来查看我的应用程序已经构建了多少。



任何人都可以帮助我创造这种外观吗?提前致谢

解决方案

您必须从 UIViewController 派生才能获得满布局控制。



然后在Storyboard中添加一个 UITableView 实例。正常约束,边缘刷新,顶部刷新,并且 customView.top = tableView.bottom 。为你的控制器做一个正常的插座。



你只需要记住使你的自定义 UIViewController 采用通常的dataSource和委托协议,以及在初始化时将自己指定为 UITableView 属性的那些角色(通常是 viewDidLoad())。



viewDidAppear()上清除所选单元格还有一个细节,但没有其他特别之处一个 UITableViewController - 它只是一个 UIViewController ,内置 tableView 属性和自动分配的代理以及非常不灵活的布局。



ADDENDUM基于如何解决这个问题的评论:这有点像询问是否有任何方法制作螺丝刀将钉子钉入木头。只需使用正确的工具。 UITableViewController 的顶级视图属性是 UITableView 本身。在布局调试模式下,请参阅下面的库存 UITableViewController ,没有别的,零代码。



这意味着整个视图树嵌入在滚动实体中,因此所有子视图都将随之滚动。



在Storyboard中重新创建这个VC真的不是什么大不了的事。毕竟,您在 cellForRowAtIndexPath 中的渲染代码以及您的dataSource和委托通常都不会发生变化。



ADDENDUM 2:



不熟悉Parse,这可能是不可能的。 (!!谢谢,Parse。)如果Parse没有为您提供可以嵌入的可分离的 PFQueryTableView ,可能会有以下解决方法,但您需要了解Objective-C代码:




I currently use a UITableViewController (PFQueryTableViewController), I would like to display a UIView over the top of the TableView.

Ideally I'd like to do this in the storyboard, so I can easily add additional Labels/buttons to it. However, I can't seem to figure out how or if you can do it in storyboard. Is that right?

I have tried it programmatically. I first created the variable at the top:

var filterLabelView:UIView = UIView()

I then added the following in ViewDidLoad :

filterLabelView.frame = CGRect(x: self.view.frame.width/2, y: self.view.frame.height/2, width: self.view.frame.width, height: 40)
filterLabelView.center = CGPointMake(self.view.frame.size.width  / 2, self.view.frame.size.height / 2)
filterLabelView.backgroundColor = UIColor.redColor()
self.view.addSubview(filterLabelView) // See below

I also tried:

self.view.insertSubview(filterLabelView, aboveSubview: tableView)

This creates the red UIView, but it seems to be embedded to the tableview, as when I scroll up and down, the View moves with it.

What I want to create

Ideally, I want the view to sit at the bottom of the screen, and not move when the user scrolls. An example screenshot of how I want it look is below:

I have read that the best way is to use a UIViewController with a UITableView inside it, but I'd rather not take that approach seeing how much my app is already built up.

Can anyone help me create this look? Thanks in advance

解决方案

You have to derive from UIViewController to get full layout control.

Then simply add a UITableView instance in Storyboard. Constrain it normally, edge-flush, top-flush, and have customView.top = tableView.bottom. Make a normal outlet to your controller.

You just need to remember to make your custom UIViewController adopt the usual dataSource and delegate protocols, as well as assigning itself as those roles to the properties of the UITableView on initialization (usually viewDidLoad()).

There's one more finesse related to clearing the selected cell when on viewDidAppear(), but there's nothing else special about a UITableViewController -- it's just a UIViewController with a built-in tableView property and automatically assigned delegates and a very inflexible layout.

ADDENDUM based on comment about how to get around this: It's sort of like asking if there is any way to make a screwdriver drive a nail into wood. Just use the right tool. The top-level view property of a UITableViewController is the UITableView itself. See below, a stock UITableViewController with nothing else, zero code, in layout debug mode.

This means the entire tree of views is embedded in a scrolled entity, so all subviews will scroll with it.

It really shouldn't be that big a deal to re-do this one VC in Storyboard. After all, your rendering code in cellForRowAtIndexPath and your dataSource and delegate generally, don't change at all.

ADDENDUM 2:

Not knowing Parse intimately, this may not be possible. (!! Thanks, Parse.) If Parse doesn't supply you with a separable PFQueryTableView that you can embed yourself, the following workaround may be possible, but you'll need to understand the Objective-C code:

https://www.parse.com/questions/pfquery-tableview-for-uiviewcontroller

这篇关于在Swift中的UITableViewController顶部添加一个UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆