Customise UITableView - iphone

How can I use more than one UITableView in one ViewController? Is it possible to ause more than one TableView in a page?
Actually i am creating a Application Form, there i have to use objects alternative for combo box.
I choose TableView instead for combo box.
Is this a right method?

Definitely Goku, you can use multiple UITableViews in a single UIViewController.
You just need to condition your both UITableView' in UITableViewDelegete.
For example :
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == yourFirstTableView) {
// <do whatever on first table view>...
} else if (tableView == yourSecondTableView) {
// <do whatever on second table view>...
}
}
Just make sure you set the both delegates for both tables :
yourFirstTableView.delegate = self;
yourSecondTableView.delegate = self;
See this example in case of doubt.

Yes you can have more than one tableview in a UIViewcontroller . You need set tag for each tableview and with respect to you can load data in the tableview .
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableview.tag == 1)
{
} else if (tableview.tag == 2)
{
}
}

Yes you can use more than one UITableView's in a single ViewController. And you can create an application form with it using different sections and using various accessory types .

You can use more than one table view in one controller, just define a different delegate for each UITableView instance. You can define new classes specifically to act as table view delegates, and have your controller create instances of these and set each UITableView's delegate property accordingly.

Yes For Sure you can use more than one tableview in single view controller.
Simply Create tableview in xib or manually.
And in methods of Tableview you can write like this
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableview==table1){
}else if (tableview ==table2){
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableview==table1){
}else if (tableview ==table2){
}
}

Related

Troubles with datasource and tableviews

I've got an UIViewController with 2 tableviews:
1- the main tableview, which is shown in the whole view controller
2- the second viewcontroller, which is loaded in a popup view.
The second tableview is shown on swiping a cell of main tableview.
Depending on which cell is swiped, there are different data in popup view.
I've already loaded the whole data in viewdidload method and stored everything in nsmutablearray, so are ready to be loaded.
My problem is that I don't know how to work with tableview's DataSource, in my project i linked both tableview's datasource to file's owner, but in this way it loads the numberofrows from the main view, and it doesn't take the correct count which should have the second tableview.
So, if in main tableview i have for example 3 elements, and in the second tableview it should load 5 elements, it gives me an error, ('NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index…).
I think that i should link my tableviews to different datasource, even if i really don't know.
I hope to be clear, if you need more info, or a better description, ask me and i'll do it for you.
In the delegate method, you should compare the tableview.
See the example,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == maintableView)
return VALUE_FOR_MAIN_TABLE_DATA;
else
return VALUE_FOR_POP_TABLE_DATA;
}
No Problem
you just set tags to your tables.
[tableView1 setTag:1];
[tableView2 setTag:2];
and then
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView.tag == 1) {
return 4;
}
else if (tableView.tag == 2)
{
return 5;
}
}
do similar thing all data source method
You can use one View Controller as an unique data source for multiple table view, but you'll need to check which table view is requesting data using the tableView arguments of the UITableViewDataSource methods.
For example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTableView)
{
// code for your main table view
}
else
{
// code for your popup table view
}
}
The same instance of a view controller serving as data source/delegate for two tables is technically possible but has a number of traps. One of these is: to which of the two tables does self.view refer?
However, most of the data source protocols hand down a reference to the table. You could use that rerference to determine which table acutally sends the request. But not all methods of the protocol inclulde a reference to the table. And that is where it starts getting tricky.
You are far better off with two distinctive view controllers, one for each table. Establish a protocol between them so that the main view controller can hand down the data to the one in the popup window so that the popup can initialize/load it self with the proper data and can refresh its view when ever the data changes.
You will need to check for the tableview in all your delegate and datasource methods as follows:
if (tableView == mainTable)
{
// code for your main table
}
else if (tableView == popupTable)
{
// code for your popup table
}
You do same for 2 or more table views.
Hope this helps.
You can do it both ways:
Make separate classes as data source for separate tables. Instantiate their objects as datasources for tables and bind them at viewDidLoad method in proper view controller.
Make one datasource for 2 tables which I don't recommend as it is not comply with proper OOAD. You'll have tight coupling this way between view controller and the table which can be cause of trouble in the near future.
You have method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
in which you can distinguish to which table you are referring:
if (tableView == mainTableView) {
// initialize cell for main table view
}
if (tableView == secondTableView) {
// intialize cell for second table view
}

Is it better to use multiple UIViewController or one controller to manage 4 tables?

I have a splitview controller and i'd like to manage more than one table in the detail view (using this cocoa control, for each section one table). Should I use 4 specific view controller or just one that controls a subview?
As per documentation :
A UITableView object must have an object that acts as a data source
and an object that acts as a delegate; typically these objects are
either the application delegate or, more frequently, a custom
UITableViewController object. The data source must adopt the
UITableViewDataSource protocol and the delegate must adopt the
UITableViewDelegate protocol. The data source provides information
that UITableView needs to construct tables and manages the data model
when rows of a table are inserted, deleted, or reordered. The delegate
provides the cells used by tables and performs other tasks, such as
managing accessory views and selections.
I would suggest to use four different UIViewController that will modularise your code and easy to managing as compared to every thing in a single UIViewController.
You can use number of table view in single base view, but you need to handle it with some tag…like tag value (any integer value) or by name. I think you need to manage with tag and this will happens like below:
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == ) {
return value;
}
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == ) {
return value;
}
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == ) {
<#statements#>
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
But in this scenarios you need to handle entire condition gently. And always care about that do not code large statement inside cellForRowAtIndexPath, another wise it will take so much time to initialize that cell at the loading time.

UItableview inside a view based application

I have a view based application and a UITableview in the view.
I set the dataSource , delegate and tableView to "File's Owner" when i click on the tableview
then clicking on the Files owner i set the tableView to Table View , view to View , datasource to Table View and delegate to Table view in the outlets.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)videoView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)videoView:(UITableView *)videoView numberOfRowsInSection:(NSInteger)section {
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)videoView:(UITableView *)videoView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [videoView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
return cell;
}
- (void)tableView:(UITableView *)videoView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
when i run the application for the simulator i get the following error
"tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6029710
"
i get the feeling that tableviews require different implementation when used in view based app rather than a navigation based application. I would be very thankful if anyone can guide me to what needs to be done to get this displayed properly.thanks.
The implementation is the same whether you are in a view-based or navigation-based application. What this error message is telling you is your table view tried to call tableView:numberOfRowsInSection: on its data source, but that the data source didn't implement a method with that name. Sure enough, looking at your sample code, you implement a method called videoView:numberOfRowsInSection: instead of tableView:numberOfRowsInSection:.
For a delegate implementation you can't change tableView to videoView. Should be:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
You need to fix the other cellForRowAtIndexPath as well!
I set the dataSource , delegate and tableView to "File's Owner" when i
click on the tableview then clicking on the Files owner i set the
tableView to Table View , view to View , datasource to Table View and
delegate to Table view in the outlets.
It's difficult to understand what you've done here. You set the table's data source to the table view itself?
when i run the application for the simulator i get the following error
"tableView:numberOfRowsInSection:]: unrecognized selector sent to
instance 0x6029710 "
What object is at address 0x6029710? It's surely the object that you set as the table's data source, but it doesn't implement the UITableViewDataSource protocol.
i get the feeling that tableviews require different implementation
when used in view based app rather than a navigation based
application.
There's really no difference between a "view based app" and a "navigation based app." There are "view based" and "nav based" project templates, but those are just two different starting points for your app. UITableView doesn't care which one you use.

iphone multiple uitableview in one view

Is there any sample project which shows multiple UITableView functionality?
I am greatly appreciative of any guidance or help.
Check this Sample project, but you need to have an account to download it:
Multiple table views on a single screen
The idea is very simple all you need to do is to distinguish between the UITableView that will trigger the delegates, for example:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == tableView1)
{
[FirstDataSource count];
}
else if (tableView == tableView2)
{
[SecondeDataSource count];
}
}
Please check out my blog post on this topic: http://iappexperience.com/post/23157217438/how-to-add-multiple-uitableviews-in-the-same-view.
Basically you can create a (second) UITableView and put it in the header (or wherever fits your scenario) of another UITableView. Then create a UITableViewController and hook it up with the UITableView in the header.

How do I populate a predefined UITableView to a view using the interface builder

I've created a custom UITableViewCell with a label and a text field.
I want to create two cells in a group to represent a nice username/password input mechanism.
I've run into some troubles figuring it out (things like the delegate/dataSource).
Is there a more direct approach to just add those two cells and get the data inserted into the text fields from code?
P.s. I want the TableView to be only at a part of my screen, the other part will be an "Enter" button...
Plus, if it can do it via the interface builder that would be great.
Although I agree with jer, some pointers about how to still use IB to make table view cells.
Add the cells to your NIB (at root level) and make them look good. Add two IBOutlet UITableViewCell *cell0, *cell1; to your header. Hook up the cells in the NIB to these outlets.
In the class which is your dataSource, do something like:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch ([indexPath row])
{
case 0: return cell0;
case 1: return cell1;
}
NSLog("Something bad happened");
return nil;
}
Interface builder doesn't know about your table view data. If you are confused about setting up a data source, then read the documentation, try the sample code, explore until things make sense. Ask when you get frustrated.