Ab'initio
Ab'initio

Reputation: 5418

Multiple table view in a single view -iPhone

I have a table view for listing users, on a button click i want to show another table view as pop up...Just like action sheets.... Any help is appreciated.... I am uploading an sample image here...!

ON Show picker button click show the table view as pop up

Upvotes: 0

Views: 2906

Answers (9)

Benny
Benny

Reputation: 5092

I'd create a separate UITableViewController for the mini table, feed it the relevant datasource once the user has selected a cell from the big table. Then add the mini table's view as a subview to the main view, maybe have a transparent UIView between the mini table and the big table so that user gestures do not get passed to the big table.

[EDIT] You can have the same View Controller be the data source and delegate for more than one table view. You may have noticed that all the UITableView's delegate methods supply a reference to the originating tableview so you can differentiate between the big and mini table, i.e. you can have a different number of rows for each table, different cells, etc.

It much the same as my suggestion above except its not as clean, you will have to have code such as the following in each delegate method:

if(tableView.tag == kBigTableTag) { ... } 
else if (tableView.tag == kMiniTableTag) { ... }

You will still need add the mini table as a subview of the View Controller's view, again as I explained above. I suggest you use a separate UITableViewController for the mini table unless you have a good reason not too?

Hope all that made sense :)

Upvotes: 2

Akshay Nalawade
Akshay Nalawade

Reputation: 1447

Lets say you have two UITableView table1 and table2-

set

table1.tag = 1;
table2.tag = 2;

set delegate and datasource property to self.

in any delegate method you can detect table by tag property. see below eg.

- (NSInteger)tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section {
    if([tableView tag] == 1)
       {
            //do stuff for table1
            return 10;
       }
    else if([tableView tag] == 2)
       {
            //do stuff for table2
            return 20;
       }

}

Upvotes: 2

Zhang
Zhang

Reputation: 11607

I would create a Custom View class with your UITableView as a subview for the following reasons:

1. Reusable

2. Easy to maintain like if you want to change the look of the view or add additional options, you only modify the custom view and all instance of it will update.

3. Delegation

That being said, the following process is how I usually create these Custom View:

1) Create a new custom view class

2) In the custom view, create a protocol at the top of the .h file, in the protocol declaration, you specify the methods that any view controller who conform to this delegate protocol must implement. For me, this usually mean I create the option chosen method and custom view closed method

@protocol CustomViewDelegate ....
    // all conforming View Controller must implement these methods.
    // in your case, you probably only need the optionChosen method
    -(void)optionChosen;
    -(void)customViewClose;
@endprotocol

3) In the @interface section of your custom view .h file, declare a "id" variable that takes a type of your protocol:

id<CustomViewDelegate> delegate;

4) Create a property for your id delegate variable

5) Create a UITableView inside your .h file

6) Create a set-entity method that takes your passed in entity as a parameter, this allow you to perform actions on this entity without sending it back to the original view controller.

7) Implement the table view inside this custom class's .m file

8) When an option is chosen, you can call the delegate method "optionChosen":

// psuedo-code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndePath:(NSIndexPath *)indexPath
{
    [delegate optionChosen];
}

9) The last thing to do is create showCustomView() and hideCustomView() in your view controller:

// psuedo code
-(void)onLongPressOnTableRow
{
    // pass the custom entity to the custom view for updating
    [customeView setEntity:[arrEntities objectAtIndex:row]];

    [self showCustomView];
}

-(void)showCustomView
{
    [UIView animateWithDuration:0.5 actions:^{
        customView.alpha = 1;
    }];
}

-(void)hideCustomView
{
    [UIView animateWithDuration:0.5 actions:^{
        customView.alpha = 0;
    }];
}

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

UITableView which main tableview and add this in your main view, After add one UIView and set as a subview of your Main View after add UITableView in your new View..

give the new view background color with black and set alpha with 0.85 or another which you see in popup view..

Give Delegate and Datasource to both UITableView and for manage this both tableview just use bellow condition ..

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == yourFirstTable) {
        return [Array1 count];
    }
    else {
        return [Array2 count];
    }
}

i hope this help you...

:)

Upvotes: 1

Shachar
Shachar

Reputation: 1168

in IB add two table views, one is the large full screen one and the other is a smaller one that has a larger Z value then the large one. Set a tag for each Table View. Set the small table view Hidden value to be YES

on every UITableView delegate method, check for Table View Tag value and continue accordingly.

if (tableView.tag == 1) {
    // load data for the large table view
}
if (tableView.tag == 2) {
    // load data for the small table view
}    

on button tap, set the small table view Hidden value to NO - this will show the small table view.

Upvotes: 0

MPG
MPG

Reputation: 251

There can 1-2 approaches to solve this but as per UX point of view its better to create a separate table for the drop-down table, set the datasource with appropriate data. when the user has selected a cell from the main table. Then add the small tableview as a subview to the main view. Now for better UX you may add a semitransparent view between these tables or set userInteractionEnable for the big table to NO while small one is visible.

Upvotes: 0

iDev
iDev

Reputation: 23278

You can add a table view as the subview of main view. Create a class of type UIViewController and add first UITableView as subview there. Then on button tap you can add second UITableView as subview of same UIViewController. You can probably bring this table view with a sliding animation from bottom and add an overlay view behind this table view.

Upvotes: 0

IronManGill
IronManGill

Reputation: 7226

This is what you have to do . On the button click , addSubView the UITableView in that particular frame. Make sure you add it on the UITableView so that it is visible. If you want you can animate it to make it look like a popoup. Hope this gives you the general idea :) ... Anything else please ask ...

Upvotes: 0

user1662392
user1662392

Reputation: 59

Add the second table view on the super view.....

Upvotes: 0

Related Questions