Wednesday 18 December 2013

Two array display in single tableview in Iphone

Table.h

@interface ip10ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *dataArr;
}

Table.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    dataArr = [[NSMutableArray alloc] initWithObjects:
                                                [[NSArray alloc] initWithObjects:@"Apple", @"Strawberry", @"Mango", nil],
                                                [[NSArray alloc] initWithObjects:@"Air Craft", @"Car", @"Bike", @"Boat", nil],
                                                nil];
}

#pragma mark - Table View Data Source


/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return @"First";

    return @"Second";
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if(section == 0)
        return @"First End";
   
    return @"Second End";
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[dataArr objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"%d, %d", indexPath.section, indexPath.row);
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   
    cell.textLabel.text = [[dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
   
    return cell;
}

#pragma mark - Table View Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            //If First Row of First Section is taped by user!
        }
    }
    */
   
    NSLog(@"%@", [[dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]);
}

No comments:

Post a Comment

Comment