UITableViewController怎么样添加一个固定的不能滚动的顶部和底部视图?

2013-05-13 17:16:18 +08:00
 qichunren
我直接addSubView新创建的视图到self.view发现是附在UITableView上的,是一起随着表格上下拖动而移动的。在不将我的现有的表格控制器提升为UIViewController的情况下,真的没有办法 在表格视图上添加一个固定的视图吗?

求解决这个问题的思路。
25557 次点击
所在节点    iDev
14 条回复
blank_dlh
2013-05-13 17:22:03 +08:00
UITableViewController 的 view 就是个 TableView ,你 add 的时候实际上就是 add 到 TableView 上了。为什么一定要用 UITableViewController 呢?
laihj
2013-05-13 17:24:31 +08:00
试试headerview?
qichunren
2013-05-13 17:25:27 +08:00
嗯,你说的我知道是那样,我从Interface Builder中看出来了。但是我现在就是不想将现有的UITableViewController提升为UIViewController,因我我使用了第三方的库,目前还不好改动。

所以想看有没有办法直接在UITableViewController的视图上添加自定义的固定视图。
qichunren
2013-05-13 17:26:42 +08:00
@laihj 我试过了呀,headerview会随着表格的上下移动而移动,它们组成了表格的整体。
Swave
2013-05-13 19:08:15 +08:00
如果只有一个section的话,用section header view和section foot view应该可以
alexrezit
2013-05-13 20:56:03 +08:00
你迟早要改过去的不如现在就改, 不改的话即使实现了也会挡住滚动条.
Gal3rielol
2013-05-13 21:52:05 +08:00
在viewDidLoad:里面这么做


- (void)viewDidLoad
{
[super viewDidLoad];

//Do the stuff about tableview
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

//Add a container view as self.view and the superview of the tableview
UITableView *tableView = (UITableView *)self.view;
UIView *containerView = [[UIView alloc] initWithFrame:self.view.frame];
tableView.frame = tableView.bounds;
self.view = containerView;
[containerView addSubview:tableView];

//add the view as a subview of the container view, it will be fixed on the top
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
topView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self.view addSubview:topView];
}
zhc
2013-05-13 22:04:11 +08:00
找到你这个controller的view既这个tableview的superview在上面放,如果找不到也不肯重构,找到最低下那个window然后。。。。
wezzard
2013-05-13 23:19:35 +08:00
可以,监听你的UITableView的delegate然后在scrollViewDidScroll:方法里面手动修正视图位置
ShengjiaWANG
2013-05-14 02:29:50 +08:00
思路:在scrollViewDidScroll方法里,用setContentOffset搞定。
ShengjiaWANG
2013-05-14 02:34:28 +08:00
或者用 @Gal3rielol 的方法,通过先把tableView加到一个view(容器)里,然后再在里面就能随便添加别的什么东西而不受tableView干扰了。

很多时候不直接使用UITableViewController,而是用UIViewController <UITableViewDelegate, UITableViewDataSource>代替,就是为的这个。
leafduo
2013-05-14 04:16:20 +08:00
外面套个 view,直接在 table view 上搞不好弄,效果也不好。

所以一般不怎么用 UITableViewController,直接用 view controller,然后自己就可以胡搞了。
dingtianran
2013-05-14 08:59:26 +08:00
把这个tableView设成只有一个section,这样这个section的header就永远在最上面了
so898
2013-05-14 09:14:36 +08:00
曾经我很天真的的幻想加在superview上面就好了……
之后我又很天真的幻想加载view.windows上面就好了……
再之后我还在很天真的幻想加载到tableview上,然后通过检测scrollview滚动来调整位置就好了……
最后……
我发现还是得提升uitablveview到uiviewcontroller……

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/68702

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX