V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
qichunren
V2EX  ›  iDev

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

  •  
  •   qichunren ·
    qichunren · 2013-05-13 17:16:18 +08:00 · 25546 次点击
    这是一个创建于 4000 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我直接addSubView新创建的视图到self.view发现是附在UITableView上的,是一起随着表格上下拖动而移动的。在不将我的现有的表格控制器提升为UIViewController的情况下,真的没有办法 在表格视图上添加一个固定的视图吗?

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

    所以想看有没有办法直接在UITableViewController的视图上添加自定义的固定视图。
    qichunren
        4
    qichunren  
    OP
       2013-05-13 17:26:42 +08:00
    @laihj 我试过了呀,headerview会随着表格的上下移动而移动,它们组成了表格的整体。
    Swave
        5
    Swave  
       2013-05-13 19:08:15 +08:00
    如果只有一个section的话,用section header view和section foot view应该可以
    alexrezit
        6
    alexrezit  
       2013-05-13 20:56:03 +08:00 via iPad
    你迟早要改过去的不如现在就改, 不改的话即使实现了也会挡住滚动条.
    Gal3rielol
        7
    Gal3rielol  
       2013-05-13 21:52:05 +08:00   ❤️ 1
    在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
        8
    zhc  
       2013-05-13 22:04:11 +08:00 via iPhone   ❤️ 1
    找到你这个controller的view既这个tableview的superview在上面放,如果找不到也不肯重构,找到最低下那个window然后。。。。
    wezzard
        9
    wezzard  
       2013-05-13 23:19:35 +08:00   ❤️ 1
    可以,监听你的UITableView的delegate然后在scrollViewDidScroll:方法里面手动修正视图位置
    ShengjiaWANG
        10
    ShengjiaWANG  
       2013-05-14 02:29:50 +08:00
    思路:在scrollViewDidScroll方法里,用setContentOffset搞定。
    ShengjiaWANG
        11
    ShengjiaWANG  
       2013-05-14 02:34:28 +08:00   ❤️ 1
    或者用 @Gal3rielol 的方法,通过先把tableView加到一个view(容器)里,然后再在里面就能随便添加别的什么东西而不受tableView干扰了。

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

    所以一般不怎么用 UITableViewController,直接用 view controller,然后自己就可以胡搞了。
    dingtianran
        13
    dingtianran  
       2013-05-14 08:59:26 +08:00
    把这个tableView设成只有一个section,这样这个section的header就永远在最上面了
    so898
        14
    so898  
       2013-05-14 09:14:36 +08:00   ❤️ 2
    曾经我很天真的的幻想加在superview上面就好了……
    之后我又很天真的幻想加载view.windows上面就好了……
    再之后我还在很天真的幻想加载到tableview上,然后通过检测scrollview滚动来调整位置就好了……
    最后……
    我发现还是得提升uitablveview到uiviewcontroller……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3192 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:35 · PVG 21:35 · LAX 06:35 · JFK 09:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.