Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 9, 2008, 09:10 AM   #1
jchildress
macrumors member
 
Join Date: Jul 2008
How do I add a UIToolBar to my UITableView view?

I've been trying to figure this out, but I'm running into some snags. I want to add a UIToolBar to the bottom of my UITableView. When I can get it to show up, the toolbar appears to be attached to the view and scrolls up and down with the table view. I want it to be anchored to the bottom of my view no matter how the TableView scrolls. I'm doing this like so, (please excuse my code writting as I'm at work and don't have xCode or my project available so my keywords are probably going to be wrong. I'm just trying to get the jist of my code across):

Code:
UITableViewController *myTableViewController = [[UITableViewController alloc] initWithTableStyle:UITableViewStyleGrouped];
UIToolBar *myToolBar = [UIToolBar new];
CGRect *toolBarFrame;
toolBarFrame = CGMakeRect (0, 440, 320, 40);
[toolBarFrame setFrame:toolBarFrame];
[myTableViewController.view addSubView:toolBarFrame];
Again, this code here is probably not 100% correct, but this is the sequence I used to add the Toolbar.

Becuase this was not what I wanted, I tried creating a new XIB file that had a tableView in the view with a toolBar ontop at the bottom of the view. When I created my UITableViewController I initilized it with the xib, but when I try to load the view in code, it crashes and complains about not finding a tableview. So I went into my XIB and assigned the delegte's view to the tableview. That then worked to load the view, but the toolbar was missing. I assume becuase I was just loading the tableView view and not the contents of the entire view of the controller.

What should be my sequence of events to add a toolbar that is anchored to the bottom of my tableView? I must be missing something here.

Thanks,
Jason
__________________
- Jason
PowerPC G5, Dual 2 GHz, 4 GB Ram, 1.5 TB HD
MacBook White, 2.4 GHz, 4 GB Ram, 160 GB HD
iPhone 3G
TV
jchildress is offline   0 Reply With Quote
Old Sep 9, 2008, 09:30 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Stop using UITableViewController. Use a standard UIViewController. Create a UIView. Add the UITableView to it sized to be the correct size (screen width, screen height-height of toolbar). Position it at top left. Add a UIToolbar to the UIView positioned at the bottom. Set the datasource and delegate of the UITableView correctly (probably as the UIViewController instance, i.e. self). Job done.
robbieduncan is offline   0 Reply With Quote
Old Sep 12, 2008, 09:42 AM   #3
jchildress
Thread Starter
macrumors member
 
Join Date: Jul 2008
I've spent some time looking into the dataSource method of filling in my UITableView with a UITableViewDataSource and for some reason I can't wrap my head around this concept to get it to work. I did some reading and looked at the example code Apple mentions in their online documentation and I can't seem to achive what they’re doing. I think, and I could be wrong, that Apple's examples are more complicated then what I need to use a dataSource for and maybe I shouldn't be using this.

What I'm trying to achieve is a UIView that contains a UITableView and a UIToolBar where the UIToolBar is anchored to the bottom or top of the view and does not move when the UITableView is scrolled. It was mentioned that instead of using a UITableViewController, I should use the standard UIViewcontroller with a UITableView and UIToolBar in the view. This way the UIToolBar is independent of the UITableView's scrolling. After doing this, I can't get the UITableView to populate itself like it automatically does with a UITableViewController. Or I can't get it to populate when I supply it with the dataSource I created.

Is there any other way to tell a table view to populate itself other then supplying a dataSource or using the initiation method "initwithStyle:"?

I'm wondering if I can create a new UIViewController class that uses a <UITableViewDelegte>, add all of my UItableView functions to the *.m file and after creating\initializing the viewController I call [myViewController setStyle:UITableViewStyleGrouped];? The documentation for the the UITableView says that the "initWithStyle" command is the starting point for firing off all the other tableview commands that fills itself out. So I'm hoping that setStyle will do the same.

If that doesn't work. can anybody recommend some example code that is a good and very simple example of creating and using a UITableViewDataSource delegate? I looked into the Apple example code "the Elements" and "UI Controls" and didn't understand it to well. Maybe there was something in those examples that I over looked.

Thanks,
Jason
__________________
- Jason
PowerPC G5, Dual 2 GHz, 4 GB Ram, 1.5 TB HD
MacBook White, 2.4 GHz, 4 GB Ram, 160 GB HD
iPhone 3G
TV
jchildress is offline   0 Reply With Quote
Old Sep 12, 2008, 10:02 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
If you post you code then perhaps we can find out what you are missing. At the moment it's just a guessing game...
robbieduncan is offline   0 Reply With Quote
Old Sep 12, 2008, 10:16 AM   #5
jchildress
Thread Starter
macrumors member
 
Join Date: Jul 2008
I'll post it tonight when I get home from work.
__________________
- Jason
PowerPC G5, Dual 2 GHz, 4 GB Ram, 1.5 TB HD
MacBook White, 2.4 GHz, 4 GB Ram, 160 GB HD
iPhone 3G
TV
jchildress is offline   0 Reply With Quote
Old Sep 12, 2008, 12:33 PM   #6
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
I've attached a project that shows a tableview and toolbar in the same view. The layout is done in IB. I don't use a UITableViewController but I'm pretty sure there are some ways to do that also.
Attached Files
File Type: zip ToolBarTableView.zip (13.5 KB, 1907 views)
PhoneyDeveloper is offline   0 Reply With Quote
Old Apr 9, 2010, 09:56 AM   #7
vikinara
macrumors newbie
 
Join Date: Mar 2010
Try this...it worked for me

Code:
(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

//Initialize the toolbar toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleDefault;

//Set the toolbar to fit the width of the app. [toolbar sizeToFit];

//Caclulate the height of the toolbar CGFloat toolbarHeight = [toolbar frame].size.height;

//Get the bounds of the parent view CGRect rootViewBounds = self.parentViewController.view.bounds;

//Get the height of the parent view. CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

//Get the width of the parent view, CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

//Create a rectangle for the toolbar CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

//Reposition and resize the receiver [toolbar setFrame:rectArea];

//Create a button UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];

[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

//Add the toolbar as a subview to the navigation controller. [self.navigationController.view addSubview:toolbar];

[[self tableView] reloadData];

}

(void) info_clicked:(id)sender {

[self.navigationController popViewControllerAnimated:YES];
 [toolbar removeFromSuperview];

}

Last edited by SilentPanda; Apr 9, 2010 at 10:19 AM. Reason: added code tags
vikinara is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Add elements to View from subclass Pades iPhone/iPad Programming 1 May 9, 2014 06:51 AM
UITableView in table view controller is cut off at the top moonman239 iPhone/iPad Programming 3 Nov 21, 2013 12:10 PM
Best way to add text labels to my view? jeanlain Mac Programming 2 Dec 16, 2012 03:00 AM
Passing Data between View Controllers : from uitableview to a details view controller EliasRahme iPhone/iPad Programming 7 Jul 20, 2012 08:04 AM
Simple PHP Website To View/Add/Delete Entries From Database Kilamite Web Design and Development 12 Jul 15, 2012 09:32 AM

Forum Jump

All times are GMT -5. The time now is 03:58 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC