0

I have a UITableViewController which contains a View and a Table View Section.

View contains a label that indicates the title of the table.

My problem is that the scroll includes the View. What I want is to keep View static (exclude from scrolling) and to scroll only Table. (I use static cells)

Thanks.

tchike
  • 154
  • 4
  • 21

3 Answers3

0

The hierarchy of a UITableViewController is

- UIView
-- UIScrollView
---- UITableView

Initially you're in the UITableView when modifying items, so you'll want to add the portion that you do not want to scroll to the UIView (outside of our scrollView). So you'll need to call super a couple times like this:

[self.superview.superview.view addSubview:viewThatDoesNotScroll];

Brayden
  • 1,795
  • 14
  • 20
  • Sorry but I didn't understand what you mean. Can you please give me more details. I use static cells and when I create custom class I get "Static table views are only valid when embedded in UITableViewController instances". Thanks. – tchike Oct 11 '12 at 16:34
  • Sorry but that is not the view hierarchy for the UITableViewController. A UITableViewController's view property points at a UITableView record. And UITableView is a subclass of UIScrollView. You should never put an UITableView object inside an UIScrollView. – J2theC Oct 11 '12 at 16:47
0

Since UITableView is a subclass of UIScrollView:

- (void)viewDidLoad {
    [super viewDidLoad];
    // mySubview is an instance variable, declared in .h file
    [self.tableView addSubview:mySubview];
    // here goes the rest of your code
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if(scrollView == self.tableView) {
        mySubview.frame = CGRectMake(mySubview.frame.origin.x, scrollView.contentOffset.y, mySubview.frame.size.width, mySubview.frame.size.height);
    }
}

The code was taken from WWDC '10 or '11 (I don't remember), so I'm sure it's the most appropriate way to do it.

Explanation: In -viewDidLoad you create your view and add it as a subview of your tableView. You can do it in -loadView or -init - it doesn't matter. The most important lines are in the -scrollViewDidScroll: method. This method is called whenever user drags the scrollView, so you can simply set the origin.y of your subview to contentOffset.y of the scrollView.

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
0

Do not UITableViewController. Use UIViewController and manage the views outside of the UITableView object. If you need, you can also implement UIViewControllerContainment to manage different views and different view controllers inside your custom view controller.

J2theC
  • 4,412
  • 1
  • 12
  • 14