Multi Table View Programmatically

                      

         Multi Table view Tapped Programmatically in IOS


here the example is for multi table view which second table view depends on first table view programmatically.
Consider two table view one is fixed(master view) and second (child view) is appear when tapped on row in master table view.
For First table view create an array (NSArray) with few objects.
For second table view create mutable array (NSMutableArray) with few object.

When any object is selected n master array then child arry will appear dynamically..
//  AppDelegate.h
//  TableView


#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end


//  AppDelegate.m
//  TableView

Leave Remaining As it is

//  main.m
//  TableView

Leave Remaining As it is



//

//  ViewController.h

//  TableView


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *masterTable,*childTable;
    NSArray *arrayMaster;
    NSMutableArray * arrayChild;
    int j;
}

@property(nonatomic,retain)UITableView *masterTable;
@property(nonatomic,retain)UITableView *childTable;
@property(nonatomic,retain)NSArray *arrayMaster;
@property(nonatomic,retain)NSMutableArray *arrayChild;

@end


//  ViewController.m
//  TableView


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize masterTable,childTable,arrayMaster,arrayChild;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor=[UIColor whiteColor];
    arrayMaster=[[NSArray alloc]initWithObjects:@"Iosbricks",@"Venkatesh",@"Nagendra",@"Satya",@"Test",nil];
    arrayChild=[[NSMutableArray alloc] init];
    NSArray * arr1=[[NSArray alloc]init];
    arr1=[[NSMutableArray alloc]initWithObjects:@"
Iosbricks
",@"27",@"Male",@"iPhone",@"Developer", nil];

    NSArray*arr2=[[NSArray alloc]initWithObjects:@"Venkatesh",@"25",@"Male",@"iPhone",@"Developer", nil];
    NSArray * arr3=[[NSArray alloc]initWithObjects:@"Nagendra",@"27",@"Male",@"iPhone",@"Developer", nil];
    NSArray*arr4=[[NSArray alloc]initWithObjects:@"Satya",@"25",@"Male",@"iPhone",@"Developer", nil];
    NSArray*arr5=[[NSArray alloc]initWithObjects:@"Test",@"25",@"Male",@"iPhone",@"Developer", nil];

    
    [arrayChild addObject:arr1];
    [arrayChild addObject:arr2];
    [arrayChild addObject:arr3];
    [arrayChild addObject:arr4];
    [arrayChild addObject:arr5];


    NSLog(@"%@",arrayChild);
    masterTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 10, 320, 200) style:
                 UITableViewStyleGrouped];
    masterTable.dataSource=self;
    masterTable.delegate=self;
    masterTable.backgroundColor=[UIColor whiteColor];
    masterTable.tag=1;
    [self.view addSubview:masterTable];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==masterTable) {
        return [arrayMaster count];
    }
    else
        
    return [arrayChild count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    for (UIView *view in cell.contentView.subviews)
    {
        [view removeFromSuperview];
    }
    if (tableView==masterTable) {
        cell.textLabel.text=[arrayMaster objectAtIndex:indexPath.row];
    }
    if (tableView==childTable)
    {
        cell.textLabel.text=[[arrayChild objectAtIndex:j] objectAtIndex:indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==masterTable)
        
    {
        j=indexPath.row;
        childTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 220, 320, 250) style:UITableViewStyleGrouped];
        childTable.backgroundColor=[UIColor whiteColor];
        childTable.dataSource=self;
        childTable.delegate=self;
        childTable.tag=2;
        [self.view addSubview:childTable];
    }
    else
    {
        NSLog(@"Tapped");
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates