How to add multiple tables in UITableViewController - iphone

Any one please tell me how to add two tables in uiview controller?
I created a class object in rootviewController as
mainDataViewController=[[MainDataViewController alloc]initWithStyle:UITableViewStylePlain];
And in mainDataviewController taken as UITableviewController
#interface MainDataViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
Now, I Want to add three tables in mainDataViewController.
Please give me some idea for solving this.

follow these steps.
1)Create a tableViewController with xib.
2)Create only two other xib's as given screenshots below:--
3)Drag uitableView from object window.
4)Change its class to TableViewController's class you have firstly created.
5)Connect file owner's view delegate to tableView.
6) And use code as ------
- (void)viewDidLoad
{
MYViewController *FirstTableController=[[MYViewController alloc] initWithNibName:#"MYViewController" bundle:nil];
MYViewController *secondTableController=[[MYViewController alloc] initWithNibName:#"MYSecondController" bundle:nil];
MYViewController *thirdTableController=[[MYViewController alloc] initWithNibName:#"MYThird" bundle:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Or
#import <UIKit/UIKit.h>
#interface MYViewController : UITableViewController
{
IBOutlet UITableView *f_table,*s_table,*t_table; //outlets for different tableViews in xib.
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.view=f_table; // when working with first table.
self.view=s_table; //working with second table.
self.view=t_table; //working with third table.
}

I think you have not searched well. Please have a look at this.
How to use 2 UITableView in a UIViewController?

1) First add tableview on your viewController xib file
2) declare variables for your tableviews
3) synthesize properthies
4) add outlets to your propethies
Now you can use them...

Related

When using StoryBoards, am I allowed to present view controllers using presentViewController:animated:completion:?

I would like to know if, when Im using storyboards, can I still present view controllers using the method presentViewController:(UIViewController *) animated:(BOOL) completion:^(void)completion ?
Or do I have to use segues?
In my project there is a VC that could be presented by any of the other VCs from the hole app, so if I would use segues, there would be like 20 segues to this same VC.
Thank you.
That should still work. You would use instantiateViewControllerWithIdentifier: to create the view controller from its storyboard definition before calling that method.
You can basically assign a StoryBoard ID to the UIViewController you want to present by any other UIViewController.
Then you have to Import the UIViewController subclass in the top of the .h file of the UIViewController that you want it to present it, for Example I've here the BaseViewController and the InfoViewController that I want to present:
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface BaseViewController : UIViewController
{
InfoViewController *InfoViewController;
}
#property (nonatomic, strong) InfoViewController *InfoViewController;
Then in the .m file you have to synthesize it and type implement the code. I'm using here a button to show the InfoViewController with an IBAction named ShowInfoAction.
#synthesize InfoViewController = _InfoViewController;
- (IBAction)ShowInfoAction:(id)sender {
InfoViewController *InfoVC = [self.storyboard instantiateViewControllerWithIdentifier:#"GiveItAnIDHere"];
[self presentViewController:InfoVC animated:YES completion:NULL];
}

How to load multiple xibs in a single xib?

I've created 4 xibs of width 200,height 200 for a purpose...but if i want to load all xib in a single xib i dont know to achieve that..I got a couple of ideas
1.Can we make all the xib to load by group of arrays?
2.Can we declare NSBUNDLE code at appdelegate.m file?..
each xib must have a class for control it. So you now must have 4 xib , 4 .h and 4 .m files. Now create another view controller with xib, .m and .h (MainViewController).
Now import all the class.
in MainController.h
#import <UIKit/UIKit.h>
#import "Xib1Class.h"
#import "Xib2Class.h"
#import "Xib3Class.h"
#import "Xib4Class.h"
#interface MainViewController : UIViewController {
Xib1Class *xib1Class;
Xib2Class *xib2Class;
Xib3Class *xib3Class;
Xib4Class *xib4Class;
}
#end
in viewDidLoad of MainViewController.m
xib1Class = [[Xib1Class alloc] initWithNibName:#"Xib1Class" bundle:nil];
xib2Class = [[Xib2Class alloc] initWithNibName:#"Xib2Class" bundle:nil];
xib3Class = [[Xib3Class alloc] initWithNibName:#"Xib3Class" bundle:nil];
xib4Class = [[Xib4Class alloc] initWithNibName:#"Xib4Class" bundle:nil];
//set the frame
xib1Class.view.frame = CGRectMake(x,y,w,h);
..........
//put in main view
[self.view addSubView:xib1class.view];
//the same for 2-3-4
remember to manage memory.
Hope this help u.

In Objective-C with no nib file is MVC to have the logic together with the view in same class?

When I work in Objective-C programatically with out nib files, and have the logic in my:
appViewController.m
having in the same class What is going on with that view, as well as with the View elements? Is this against the MVC pattern?
Do I have to create another class and message both classes?
It's up to you! If you want to separate layers (M,V,C) you can create your own view programmatically and, by using composite design pattern, build it in your UIView subclass, by removing drawing code from your controller.
That is...
You create a "CustomCompositeView" that extends UIView
in layoutSubview (hinerited from UIView) you will draw all your UI elements
in your CustomViewController you will display your view using loadView:
code:
- (void)loadView
{
CustomCompositeView *mainView = [[CustomCompositeView alloc] initWithFrame:aFrame];
[self setView:mainView];
[mainView release]; // remove this line if you are using ARC!
}
Technically, it is going against the MVC pattern. Your V and C and combined into a single object. You can seperate the code that handles layout and drawing into a seperate UIView subclass. Then load it with loadView:
// MyViewController.m
- (void)loadView {
MyView* myView = [[[MyView alloc] init] autorelease];
myView.delegate = self;
self.view = myView;
}
#pragma mark - MyViewDelegate Methods
- (void)myViewSaveButtonWasPressed:(MyView *)myView {
// do something
}
To communicate between the view and the view controller, you can define a delegate protocol.
// MyView.h
#class MyView;
#protocol MyViewDelegate <NSObject>
- (void)myViewSaveButtonWasPressed:(MyView *)myView;
#end
#class MyView : NSObject
#property (nonatomic, assign) id<MyViewDelegate> delegate;
// ...
When a button is pressed in the view (or something else along those lines) pass that on to the delegate. The ViewController should conform to the delegate method and handle the actual logic itself that way.

adding a UITableView programmatically to a UIViewController

I'm loading a UIViewController into one of my Nav controller's hierarchies, which will contain some text and some images. At the bottom, I will want to create a expandable and collapsable tableview.
First off, is this idea possible? If it is, how do I add it and where do I place the data source and delegate methods?
Can I just make a separate subclass of the TableViewController and then add it to my ViewController as a subview?
Yes, you can create a UITableView whose delegate, datasource, and parent view are not necessarily a UITableViewController. Since the UITableView is a UIView, you can add it as a subview of any other UIView. Any NSObject can be the delegate or datasource, as long as you implement the required protocol methods.
#interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
In fact, in my experience, not many people even use UITableViewControllers. When was the last time you wanted your table view to take up the entire usable space? In general, I create a plain old UIViewController and add a UITableView as a subview of its view, in addition to other subviews.
/************************************************/
/************* MyCustomController.m *************/
/************************************************/
#interface MyCustomController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) UITableView *tableView;
#end
#implementation MyCustomController
- (id)initWithNibName:(NSString*)nibName bundle:(NSString*)bundleName
{
self = [super initWitNibName:nibName bundle:bundleName];
if (self)
{
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.datasource = self;
tableView.delegate = self;
[self.view addSubview:self.tableView];
}
return self;
}
#pragma mark - UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return number of rows
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return cell
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// handle table view selection
}
#end
It's pretty easy, in something like your viewDidLoad method:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:tableView];
Just remember that a UITableViewController is a subclass of UIViewController only with the tableview set as the controller's view.
So yes definitely possible and used quite frequently when you want to have a tableview but also other custom UI elements which prevent you from using the UITableViewController.
I'd normally choose to add it to my view controller's view in either its initialisation method or viewDidLoad method. This will vary based on whether you're creating your views from a NIB or entirely programatically.
In case of NIBs:
- (id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)bundleName
{
if ((self = [super initWitNibName:nibName bundle:bundleName]))
{
self.theTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewWhateverStyleYouWantHere];
theTableView.dataSource = self, theTableView.delegate = self;
[self.view addSubview:theTableView];
[theTableView release];
}
}
And then you can set the frame of your tableview in your viewDidLoad method.
I'd personally prefer to do the whole thing in interface builder as you'd achieve the same result with way less code to maintain.
If you're like me and already had created a UITableViewController and then realizing that you did so much work on it that re-writing it would be a pain, you can just do the following to add the UITableViewController to the UIViewController as a subview.
UITableViewController* tableViewController = [[UITableViewController alloc] init];
[self.view addSubview:tableViewController.tableView];
All the other answers above works great. I figure I'd add to this for those that have a heavily invested implementation of a UITableViewController and feel like refactoring would be a pain.

access a property in UIView in Viewcontroller?

I have been given a project to edit. I think this is a simple question but want to explain it in detail.I usually set up iPhone projects with interface builder and then have a view controller h and m file.
However this has been set up in a different way I am new to, the view has been coded.
The h file is a simple viewcontroller class like this:
#interface MainViewController : UIViewController
{
}
- (id)init;
#end
And then the m file has this:
#interface MainView : UIView
{
NSUinterger firstinterger;
}
- (id)initWithImages:(NSArray *)inImages;
#end
And then it has the #implementation MainView just after that with lots more code.
Further down however is where I need to add my code just after
#end
#implementation MainViewController
But I need to access the NSUinterger named first integer and I am unable to. I have tried a few ways of synthesizing etc. but I think I am doing it wrong. How I would get the value of it? I can access it in the code before the #implementation MainViewController but not after which is where I need it.
Synthesize the variable in MainView. Have an instance of the MainView in MainViewController and then you can access it by
MainView *mv = [[MainView alloc] init];
mv.firstInteger // gives you the variable.
NSUinterger firstinterger in your code shows no ';' at the end of that line, do you get compilation errors or is it just a typo in your question?