UITableViewController - limit the scrolling - iphone

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.

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];

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.

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.

Related

How can I be notified when a TableView has been removed from the super view?

I have programmed in several other languages but this is basically my first ios app and I am struggling to correctly implement a UITableView.
After reading the documentation, the most common way to accomplish this is to create a class that is a subclass of UITableViewController. I have done this and I have implemented all data source protocol methods as well as the row selection method from the delegate protocol and gave it three properties.
The first one is the number of rows in the tableview,
the second is an array of the items to be displayed as labels in the table view,
and finally there is a property to hold the text of the label from the selected row.
Once the row is selected, I set the property of that holds this label and then I remove the table form the view with [self.view removeFromSuperView].
The above isn't the only view in my app. The app is a color picker assignment, from school, so the main view contains all of the controls to manipulate the displayed color.
What I did after subclassing UITableViewController was, create an instance of this subclass in my main view controller and made it a property. So, on the main view is a recall button that allows the user to choose from a list of previously saved colors. When this button is clicked the this IBAction method is called
-(IBAction)swithToSavedColorsView:(id)sender {
self.savedColorTable.numberOfRows = self.dictionaryOfSavedColors.count;
NSLog( #"Count in switch view is %d", self.dictionaryOfSavedColors.count );
[ self.view addSubview:self.savedColorTable.view ];
}
This presents a list of the available saved colors and I respond to the row selection with
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
self.textFromColorSelection = [ [ NSString alloc ] init ];
UITableViewCell *cell = [ tableView cellForRowAtIndexPath:indexPath ];
self.textFromColorSelection = cell.textLabel.text;
NSLog(#"The value of selection is %# ", self.textFromColorSelection );
[ self.view removeFromSuperview ]; // Go back to main screen.
}
As I was writing this code I was getting an erie feeling that I went about the creation of the UITableView in the completely wrong way from the beginning. Please let me know if I am doing something wrong as far as how I have communication between these objects set up.
The problem I am actually trying to solve is in the above method after i call
[ self.view removeFromSuperView ], how will my other view know when this has happened? What I want to do when the UITableView closes is have my other view get the label property from the instance I created and use that label to retrieve information out of a database.
Thanks for the help, it is greatly appreciated.
If you need several controllers to respond to the dismissal of the table, you will likely want to use NSNotificationCenter in viewWillDisappear or viewDidDisappear. More likely you are presenting from a viewController and only that viewController is waiting to learn what color was selected. I would suggest handling that with a delegate/protocol pattern.
Add something like this to your color pick table myColorPickTableController.h file above the #interface line:
#class myColorPickTableController;
#protocol myColorPickTableControllerDelegate
-(void)myColorPickTableControllerDidSelectColor:(UIColor *)selectedColor sender:(myColorPickTableController *)sender;
#end
Add a property in that header as well, to store reference to the delegate (the controller which is waiting to hear what color was picked).
#property(nonatomic, unsafe_unretained)id<myColorPickTableControllerDelegate> delegate;
Now, replace the line
[ self.view removeFromSuperview ]; // Go back to main screen.
with
[delegate myColorPickTableControllerDidSelectColor:[UIColor whateverColorWasPicked] sender:self]; // Tell main screen user picked a color
Now in the presenting controller, you need to conform to the protocol by adding to your interface line
#interface myPresentingController : UIWhateverControllerIAm <myColorPickTableDelegate> // Add that part between <>
Now, in myPresentingController.m you implement the method
-(void)myColorPickTableControllerDidSelectColor:(UIColor *)selectedColor sender:(myColorPickTableController *)sender
{
[self saveTheSelectedColor:selectedColor];
[sender.view removeFromSuperview]; // I am not so sure about that, should be presenting, maybe modally or use navigation controller. Should work thought, not my first choice.
sender = nil; // Just for good measure
}
Lastly, remember to make your presenting controller the delegate of the myColorPickTableController when you create it. Set delegate as self like so
myColorPickTableController *pickTable = [myColorPickTableController alloc] init];
pickTable.delegate = self.
In your subclassed UITableViewController, viewWillDisappear and viewDidDisappear should be called after your view is removed. Try sending a notification when your view disappears (look into NSNotificationCenter).

iOS 7 Table view fail to auto adjust content inset

I am transiting my project to iOS7. I am facing a strange problem related to the translucent navigation bar.
I have a view controller and it has a tableview as subview (let's call it ControllerA) . I init a new uinavigationcontroller with the controllerA and present it modally using presentviewcontroller. The presented view controller's table view is blocked by the navigation bar. I set the automaticallyAdjustsScrollViewInsets to YES but the result did not change.
I knew I can set the edgesForExtendedLayout to UIRectEdgeNone, but it will make the navigation bar no more translucent.
After that, I tried to create a new view controller for testing. It contains almost the same elements. But the result is much different. The table view content does not get blocked.
Conclusion
Two View Controllers' automaticallyAdjustsScrollViewInsets set to YES
The project is not using storyboard
The first one is created at Xcode 4.6, The second one is newly created on Xcode 5
I have compared two classes xib and code, not much different
I have found the answer on apple developer forum.
There are two different case.
The first one, the view controller added is a UITableViewController.
And the issue should not be appeared since apple will auto padding it.
The second one, the view controller is NOT a UITableViewController.
And in the view hierarchy, it contains a UITableView. In this case, if the UITableview(or ScrollView) is the viewController's mainview or the first subview of the mainview, it will work. Otherwise, the view controller doesn't know which scroll view to padding and it will happen the issue.
In my case, the view controller is the second one. And there is a background image view as the first subview of the main view. So, it fails.
Here is the Apple developer forum link (need developer account to access):
https://devforums.apple.com/message/900138#900138
If you want the view to underlap the navigation bar, but also want it positioned so the top of the scrollview's content is positioned below the navigation bar by default, you can add a top inset manually once the view is laid out. This is essentially what the view layout system does when the top-level view is a scroll view.
-(void)viewDidLayoutSubviews {
if ([self respondsToSelector:#selector(topLayoutGuide)]) {
UIEdgeInsets currentInsets = self.scrollView.contentInset;
self.scrollView.contentInset = (UIEdgeInsets){
.top = self.topLayoutGuide.length,
.bottom = currentInsets.bottom,
.left = currentInsets.left,
.right = currentInsets.right
};
}
}
Based on Tony's answer I was able to get around this problem programatically with temporarily sending the table view to the back, let the adjustments be made and then send the background view back to the back. In my case there is no flickering to this approach.
In the View Controller:
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self.view sendSubviewToBack:self.tableView];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.view sendSubviewToBack:self.backgroundView];
}
Obviously if there are other subviews on self.view you may need to re-order those too.
There's probably too many answers on this already, but I had to take Christopher's solution and modify it slightly to support view resizing and allowing the content inset to be changed in a subclass of the UIViewController.
#interface MyViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (assign, nonatomic) UIEdgeInsets scrollViewInitialContentInset;
#end
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setScrollViewInitialContentInset:UIEdgeInsetsZero];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
if (UIEdgeInsetsEqualToEdgeInsets([self scrollViewInitialContentInset], UIEdgeInsetsZero)) {
[self setScrollViewInitialContentInset:[self.scrollView contentInset]];
}
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIEdgeInsets scrollViewInset = [self scrollViewInitialContentInset];
if (UIEdgeInsetsEqualToEdgeInsets(scrollViewInset, UIEdgeInsetsZero) {
if ([self respondsToSelector:#selector(topLayoutGuide)]) {
scrollViewInset.top = [self.topLayoutGuide length];
}
if ([self respondsToSelector:#selector(bottomLayoutGuide)]) {
scrollViewInset.bottom = [self.bottomLayoutGuide length];
}
[self.scrollView setContentInset:scrollViewInset];
}
}
#end
To explain the point:
Any subclass of MyViewController can now modify the contentInset of scrollView in viewDidLoad and it will be respected. However, if the contentInset of scrollView is UIEdgeInsetsZero: it will be expanded to topLayoutGuide and bottomLayoutGuide.
#Christopher Pickslay solution in Swift 2:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let topInset = topLayoutGuide.length
inTableView.contentInset.top = topInset
inTableView.contentOffset.y = -topInset
inTableView.scrollIndicatorInsets.top = topInset
}
Yeah - a bit annoying.
I have a nib with a single tableview within the main view, not using autolayout. There is a tabbar, navigationbar and a statusbar and the app needs to work back to 5.0. In Interface builder that neat 'see it in iOS7 and iOS6.1 side-by-side' thing works, showing the tables neatly fitting (once the iOS6/7 deltas were set properly).
However running on a device or simulator there was a large gap at the top of the table, which was as a result of a content inset (which pretty much matched by iOS6/7 vertical delta) that was set to zero in the nib.
Only solution I got was in viewWillAppear to put in [_tableView setContentInset:UIEdgeInsetsZero].
Another ugly hack with a pretty on-screen result.....
[self.navigationController setNavigationBarHidden:YES animated:YES];
in:
- (void)viewDidLoad

iphone development: how to put a UIView over UITableView [duplicate]

This question already has answers here:
How to add a UIView above the current UITableViewController
(21 answers)
Closed 8 years ago.
In my app I use storyboard. One of my elements in the storyboard is a UITableViewController. So it has a tableview inside of it.
My question is how can I put a UIView over this tableview. It is gonna be hidden and I want to make it visible when a tableviewcell in the tableview is pressed. Is that possible? How can I do that?
The best solution is use normal view controller (UIViewController) in StoryBoard. Your controller will need to implement two protocols (UITableViewDataSource and UITableViewDelegate) and you will need add UITablewView in the view controller's view.
In this case in interface builder you will be able to put any view in the view controller's view (can put it above table view).
Use tableHeaderView property.
Returns accessory view that is displayed above the table.
#property(nonatomic, retain) UIView *tableHeaderView
The table header view is different from a section header.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Lets assume your view to be hidden/shown on top of table view is topView, declared as a global variable.
Declare topView in .h as
UIView *topView;
Now Lets assume that you have the UITableViewController object as tableViewController then, initialize the topView in viewDidLoad of tableViewController class
-(void)viewDidLoad
{
topView=[[UIView alloc] initWithFrame:yourNeededFrameSize];
[self.tableView addSubview:topView];//tableView is a property for UITableViewController inherited class
topView.hidden=YES;//Hide it initially for the first time.
}
assuming that you have the UITableViewDelegate methods implemented here is what you will do in didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(topView.isHidden==YES)
{
topView.hidden=NO;
}
else
{
topView.hidden=NO;
}
}
hope it helps.
you can also get view into front.
[view bringsubviewtofront];
I had a similar problem where I wanted to add a loading indicator on top of my UITableViewController. To solve this, I added my UIView as a subview of the window. That solved the problem. This is how I did it.
-(void)viewDidLoad{
[super viewDidLoad];
//get the app delegate
XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//define the position of the rect based on the screen bounds
CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50);
//create the custom view. The custom view is a property of the VIewController
self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect];
//use the delegate's window object to add the custom view on top of the view controller
[delegate.window addSubview: loadingView];
}

Why UITableView calls UIScrollView's delegate methods?

I have an application, in which I have a UITableView inside a UIView, which is again inside a UIScrollView, so the hierarchy becomes like this:
UIScrollView -> UIView -> UITableView
The data inside my UITableView is filled properly.
Now, my problem is that, When I scroll my UITableView, the UIScrollView's delegate method scrollViewDidEndDecelerating: and scrollViewDidEndDragging:: gets called.
I don't want this behavior, what should I do to stop this behavior?
Any one please Help,
Thank in advance!!!
UITableViewDelegate extends UIScrollViewDelegate. Hence the calling of the delegate methods.
To stop this you can set tableView.tag = 1000; when you alloc the tableView and in the UIScrollViewDelegate methods ( scrollViewDidEndDecelerating: and scrollViewDidEndDragging:: )add this at the very begining:
if(scrollView.tag == 1000)
return;
Because UITableView inherits from UIScrollView. So it shows all the properties and behaviours of UIScrollView. If you dont want this then please do one thing.
Assuming you have another scrollview in your page.
in the viewDidLoad or from the XIB (if you have your tableview in the XIB), set a tag for your tableview.
in code,
self.objYourTableView.tag = 101;
then in the scroll view delegate
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
if(!(scrollView.tag == 101))
{
// Go for your code to run.
}
}
So that your code will skip if it called by the table view. Other cases it works perfect. Hope this will help you.

Resize table view

I'm searching for a way to have a UITableViewController with a UITableView at the top and a UIPickerView bellow (with fix position).
I've found a solution for fixing the picker with the code bellow:
- (void)viewDidLoad {
[super viewDidLoad];
_picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
_picker.showsSelectionIndicator = YES;
_picker.dataSource = self;
_picker.delegate = self;
// Add the picker to the superview so that it will be fixed
[self.navigationController.view addSubview:_picker];
CGRect pickerFrame = _picker.frame;
pickerFrame.origin.y = self.tableView.frame.size.height - 29 - pickerFrame.size.height;
_picker.frame = pickerFrame;
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.height = 215;
self.tableView.frame = tableViewFrame;
[_picker release];
}
The problem is with the tableview, it seems resizing doesn't work so I can't see all results .
Thanks for your advice.
You should use a UIViewController subclass instead of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, one of which is a table view. You can add a UITableView subview and make your controller implement UITableViewDelegate and UITableViewDataSource protocols.
The default behavior of the UITableViewController class is to make the table view fill the screen between the navigation bar and the tab bar (if either are present).
From Table View Programming Guide for iOS:
Note: You should use a
UIViewController subclass rather than
a subclass of UITableViewController to
manage a table view if the view to be
managed is composed of multiple
subviews, one of which is a table
view. The default behavior of the
UITableViewController class is to make
the table view fill the screen between
the navigation bar and the tab bar (if
either are present).
If you decide to
use a UIViewController subclass rather
than a subclass of
UITableViewController to manage a
table view, you should perform a
couple of the tasks mentioned above to
conform to the human-interface
guidelines. To clear any selection in
the table view before it’s displayed,
implement the viewWillAppear: method
to clear the selected row (if any) by
calling deselectRowAtIndexPath:animated:.
After the table view has been
displayed, you should flash the scroll
view’s scroll indicators by sending a
flashScrollIndicators message to the
table view; you can do this in an
override of the viewDidAppear: method
of UIViewController.