Prabakaran
Prabakaran

Reputation: 258

How to work in two UITableView in a single View Controller in ios

How to work in two UITableView in a single View Controller in ios.In my app i designed the textfield and the buttons in a tableview(FirstTableVie).And i try to implement the comboBox in one of the textfield.Here the i implement the delegate function for comboBox tableview(SecondTableView), while i run this it shows the SecondTableView the FirstTableView is not displaying.So hlep me to implement the two bale view in a single ViewController.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView==self.FirstTableView)
    {
        return 0;
    }
    else if(tableView==self.SecondTableView){
        return [stateArray count];
    }
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    if(tableView==self.FirstTableView)
    {
    }
    else if(tableView==self.SecondTableView)
    {
        static NSString *identifier=@"Cell";
        cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if(cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text=[stateArray objectAtIndex:indexPath.row];
    }
    return cell;
}

Upvotes: 0

Views: 1460

Answers (4)

LanternMike
LanternMike

Reputation: 664

Maybe just create custom class/objects that you set the delegate methods to let them own it and implment the delegate methods in your custom classes.

i.e.

@interface TableViewHandler : NSObject
@end
@implmentation
// implement delegate methods
@end

then initialize and allocate these objects and assign them to the delegates of your two table views.

so in your view controller you can do

tableViewHandler *one = [[TableViewHandler alloc] init]; // you can implment an init if you want otherwise it goes to the NSObject init

and then assign the delegate methods of your first table view to this object.

so instead of the typical in a viewController

myTableview.delegate = self

you would do

myTableView.delegate = self.one; 

and of course dataSource as well.

The advantage of this over if this table view or that is you can segregate the code.

It depends if you want re-usability of code. There is less re-usability in ViewControllers that are structured with if this class or that or that. Custom classes can be cleanly modified and carried around to other projects as needed. For example if you want to use one of these tables in another project you'd be messing around with copying and paste from the all the delegate methods you add the if else logic to. If you isolate the code you can just port the file.

Upvotes: 1

Vidhyanand
Vidhyanand

Reputation: 5369

You can check like below.I wrote only the conditions not entire code..

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView==self.FirstTableView)
    {
        return 0;
    }
    else if(tableView==self.SecondTableView){
        return [stateArray count];
    }
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     UITableViewCell *cell;
     static NSString *identifier=@"Cell";
    cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell==nil)
    {
      cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if(tableView==self.FirstTableView)
    {
    }
    else if(tableView==self.SecondTableView)
    {
        cell.textLabel.text=[stateArray objectAtIndex:indexPath.row];
    }
    return cell;
}

Hope it helps you..

Upvotes: 1

SSemashko
SSemashko

Reputation: 1497

Actually I don't see a problem here. Every delegate and dataSource protocol method returns a refernce to a prticular TableView. Just set you view controller as a delegate to both tables and find out which of two is used in callback. You can distinguish tables by tag.

Example:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 1) {
        return 5;
    }else if (tableView.tag == 2){
        return 10;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (tableView.tag == 1) {
        cell = [self configreCellForFirstTableAtIndexPath:indexPath];
    }else if (tableView.tag == 2){
       cell = [self configreCellForSecondTableAtIndexPath:indexPath];
    }
    return cell;
}

Upvotes: 0

Ramesh Muthe
Ramesh Muthe

Reputation: 811

Add tags to two tableview and check the if the condition while handling in the table view delegate like

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView.tag==1) {
   } else {
    }

}

I just posted the logic how to approach

Upvotes: 0

Related Questions