tableView initwithframe and storyboard - ios

I'm trying to add a tableview as subview to my tableViewController, but I want to setup the cells in storyboard. It will be a static tableview.
This is the code for calling the tableview on button click.
- (IBAction)botaoAdicionar:(id)sender {
AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(0, 170, 320, 320) style:UITableViewStylePlain];
[self.view addSubview:tableview];
}
In the tableview class I have this:
#implementation AtividadesPraticadasTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Now, I have a viewcontrollerin storyboard with a tableview, which the class of the tableviewI changed to this file AtividadesPraticadasTableView. It has three static custom cells in storyboard, therefore it opens a blank default tableview.
What am I missing?

Static table views are entirely contained within the storyboard, and require information from the storyboard to display their content.
You've defined a static table view controller in the storyboard, populated it and set the tableView's custom class to your custom class, but when you want to add the table view you are just instantiating a table view of that class. That isn't going to get any of the information you've added to the storyboard.
In addition, the static cells information is read and understood by the UITableViewController, not the UITableView, so you are at the wrong level there too.
You need to do the following:
Get a reference to the storyboard, either from your original view controller's storyboard property (if it is on the same storyboard as your static table) or using storyboardWithName:bundle:.
instantiate the table view controller from that storyboard, using instantiateViewControllerWithIdentifier:. This will create a table view controller object containing all your static cells
Add this as a child view controller to your original view controller, using addChildViewController:
Add the table view controller's tableView as a subview
It may be simpler to add a container view in the storyboard to hold this view, and reveal it when the button is pressed, as Mike Slutsky suggested - this will do all of the instantiating and adding and child view controller-ing work for you, but the principle is still the same.
Also, adding a table view as a subview to a table view controller sounds very dodgy - a table view controller already has a table view as its view, and you can't really add subviews to that.

The thing your missing is the association between the programatically instantiated tableview and the UITableView that you put in your storyboard. You cannot just draw UITableViews in your storyboard and start instantiating new UITableViews in the controller's code and expect xcode to know which UITableView you wanted to use from the storyboard. Use an IBOutlet to connect a global variable for the controller to the UITableView in the storyboard, then the controller will know what you're trying to refer to. If you want that UITableView to appear on a button click, simply set the UITableView to hidden by default in the storyboard and then unhide it when the button is pressed.

The thing You are missing called manual. Check this protocol for TableView dataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource
P.S. Here good tutorial for storyboards http://maniacdev.com/ios-5-sdk-tutorial-and-guide/xcode-4-storyboard

Related

Add extra static tableview to UITableViewController

I have a UITableViewController with a dynamic tableView. Now, I want to add a second tableview as subview to my UITableViewController on button click, but I want to design this static tableView using storyboard. So I added another UITableViewController and changed the class of the tableView to my custom class. But when I instantiate it on my firs TVC, it doesn't load the one from storyboard. I think it just instantiates a new object of my custom class but not the one on the storyboard file. And I can't connect the outlet of the tableView to my first TVC, it doesn't let me.
So I guess my option here would be to implement a new UITableViewController and add it as subview, instead of just adding the table, right? I just thought I didn't need another UITableViewController and could just use the tableview but I guess I'm wrong.
Any suggestions?
UPDATE:
This code works fine, I'm just wondering if this is the right approach.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Treinamento" bundle:nil];
TabRelTableView *tabRelatorio = [sb instantiateViewControllerWithIdentifier:#"TabRelTableView"];
tabRelatorio.tableView.frame = CGRectMake(0, 38, 320, 466);
[self addChildViewController:tabRelatorio];
[self.view addSubview:tabRelatorio.tableView];
TabRelTableView is a UITableViewController.
Have you thought of view controller containment? This way you can have a custom controller with dynamic table view as well as storyboard-designed static tableView. Then you can switch between dynamic and static ones based on your needs.

Reusing a UITableView footer view in multiple views

I have a footer for a UITableView which is a "complex" view (textView and a button).
I made a xib and added it to the footer of the UITableView.
The next stage is having referencing outlets in the ViewController of the UITableView
The thing is that I want to use this footer view in several ViewControllers (and not DRYing)
What can I do?
It sounds like you want to have a xib/.h/.m separate from the .xib used by your UIViewController. This is for your View that will be used as the footer of the UITableView. You can [[MyFooterView alloc] init] in the viewDidLoad of any controllers that are using a UITableView and assign the footer to that instance of MyFooterView. Is this what you're after? You can still have referencing outlets to the UITableView in any UIViewController, and keep a reference to MyFooterView that you instantiated in viewDidLoad
addTarget example:
myFooterView.button addTarget:self action:myCustomHandlerFunction forControlEvents: UIControlEventTouchUpInside

Add view behind tableview in UITableViewController

I'm trying to add this custom control below my tableview in a TableViewController:
https://github.com/zogieosagie/RMEIdeasPullToSortControl
In the example the creator gives, the control is implemented using a ViewController and an added tableview, but I want to use it in a TableViewController. I have created and initialized it as shown in the example but I cannot get it to show up behind the table. Any ideas?
Here is a screenshot of the control above my tableview: https://www.dropbox.com/s/ojfpacxelcy9cqm/Photo%20May%2028%2C%208%2057%2035%20PM.png
Here is my code in the viewDidLoad method:
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.rmeideasPullDownControl = [[RMEIdeasPullDownControl alloc] initWithDataSource:self delegate:self clientScrollView:self.tableView];
self.sortTitlesArray = [[NSArray alloc] initWithObjects:#"Listed from A - Z", #"Listed from Z - A", #"Brand value: HIGHEST - LOWEST", #"Brand value: LOWEST - HIGHEST", #"Founded: OLDEST - NEWEST", #"Founded: NEWEST - OLDEST", nil];
CGRect originalFrame = self.rmeideasPullDownControl.frame;
self.rmeideasPullDownControl.frame = CGRectMake(0.0, 45.0, originalFrame.size.width, originalFrame.size.height);
//It is recommended that the control is placed behind the client scrollView. Remember to make its background transparent.
//[self.view insertSubview:self.rmeideasPullDownControl belowSubview:self.tableView];
[self.tableView addSubview:self.rmeideasPullDownControl];
[self.tableView sendSubviewToBack:self.rmeideasPullDownControl];
Table view controllers do not lend themselves to managing anything other than a table view. In a table view controller the content view of the view controller is the table view.
You should not try to add other views as subviews of a table view.
Those 2 things combined mean that you can't do what you are trying to do.
Instead, you should create a regular UIViewController. In your storyboard, add a container view to the view controller's content view. Create a UITableViewController as a separate scene, and then control-drag from the container view onto the table view controller. That will set up an embed segue, so your table view controller becomes a child view of the regular view controller. Now you can do whatever you want to the main view controller's content view, including adding other views behind the table view.
Do you mean that you are using a Table View Controller on the storyboard? Or do you mean that your backing code is a subclass of UITableViewController?
I haven't used this project before but I'm guessing you are using a Table View Controller on the storyboard, in which case there is no backing view for the RMEIdeasPulldownControl to attach to (the top-level view is a UITableViewController). If you look in the example it needs to be attached to a scrollview (like a table view) but it needs to be inserted into a view (like a UIView)
If you meant the second one then I'm not sure, UITableViewControllers are subclassed from UIViewControllers and are really very similar, so I can't imagine any trouble arising from that.
It isn't possible directly, but you can create UIViewControllerClass with relevant storyboard UIViewController
add a MyUIView in hierarchy then UITableView next to MyUIView
attach datasource and delegates for UITableView and use MyUIView as per your requirement.

UITableViewController with TableView and a non scrolling footer view

I'd like to implement something that looks like a media player, a playlist in table view that takes 3/4th of the screen height and a 1/4 height fixed panel at the bottom for play controls. I can't add anything to my UITableViewController in Storyboard, it only allows adding something to my TableView. How should this be done in iOS?
A UITableViewController inherits fromUIViewController. There is two possibilities to add others element with your tableview :
In the viewDidLoad method of your table view controller, replace it's view (it is its table view at the moment) by a new view, resize the table view, and add a view at the bottom programmatically.
Replace your UITableViewController in your storyboard by a simple UIVIewController. You add in it your tableview, your bottom view, and everithing else you want. In the header file of your view controller, specified it implements the delegates methods for a tableview :
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
And don't forget to assign the delegate and datasource of your tableview in the IB to this view controller.
Then write the methods required for a tableview datasource and delegate (tableView:cellForRowAtIndexPath: , numberOfSectionInTableView:,...)

uitableview inside multiple view controllers

I have to 'repeat' a uitableview into 2 or more view controllers. Like using a uitableviewcontroller with its methods inside view controllers using the methods/delegates/datasources from the uitablewviewcontroller.
Example: I've a uitableviewcontroller wich displays a twitter feed with its own style, so I need this table in more than 1 view controller and maybe just changing the twitteruser in each view controller (just an example).
TRY: What I've done is to create (with storyboard) a uiviewcontroller and a uitablewviewcontroller, both with their own methods. And on uiviewcontroller viewdidload, try to add the uitablewviewcontroller.tableview as a subclass. and this works! But the result is an empty table. I tried to set the delegates/datasources but it ddnt work..
-(void)viewDidLoad
{
[super viewDidLoad];
//Add Table as subview
thetable *t = [[thetable alloc] init];
[self.view addSubview:t.tableView];
t.tableView.frame = CGRectMake(0, 100, 320, 2000);
t.tableView.backgroundColor = [UIColor blueColor];
}
This code works, but just displays an empty uitable inside the view controller..
*thetable is a uitableviewcontroller object (.h, .m, and storyboard view)
*Just set the backgroundcolor to check if there's anything on screen
*using ios6
Thanks!
Make sure you are implementing the table view delegate and datasource methods in thetable.Also try [t.tableView reloadData] after adding the tableView to the view controller's view.

Resources