Wednesday 18 December 2013

UITableView display data using Array and table cell in Iphone

Table.h

@interface ip11ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSArray *controlsArr;
}

@property (nonatomic, retain) IBOutlet UITableViewCell  *cell0,
                                                        *cell1,
                                                        *cell2;

Table.m

@synthesize cell0,
            cell1,
            cell2;
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    controlsArr = [[NSArray alloc] initWithObjects:cell0, cell1, cell2, nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return 3;
   
    return controlsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    UITableViewCell *tempCell;
   
    if(indexPath.row == 0)
        tempCell = cell0;
    else if(indexPath.row == 1)
        tempCell = cell1;
    else if(indexPath.row == 2)
        tempCell = cell2;
   
    return tempCell;
     */
   
    return [controlsArr objectAtIndex:indexPath.row];
}

No comments:

Post a Comment

Comment