Popular Posts

May 23, 2011

Show Data In Table View Using Arrays


-Make a project iTableView
- In RootViewController.h We will declare a mutable array

//
//  RootViewController.h
//  iTableView
//
//  Created by Jubayer Ahmed on 5/22/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
NSMutableArray *datas;
}
@property(nonatomic, retain) NSMutableArray *datas;

@end


- In RootViewController.m we synthesize the variable and  initialize the array

//
//  RootViewController.m
//  iTableView
//
//  Created by Jubayer Ahmed on 5/22/11.
//  Copyright __MyCompanyName__ 2011. All rights reserved.
//

#import "RootViewController.h"


@implementation RootViewController
@synthesize datas;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
          NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:@"Dhaka",@"Chittagonj",@"ManikGonj",nil];
     self.datas = tempArray;
          [tempArray release];
        
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- In Root.h file numberOfSectionsInTableView and numberOfRowsInSection tells the table view how many sections we have and how many rows in each section.


// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.datas count];
}

- indexPath: The indexPath contains the section and row number for the needed cell
-cellForRowAtIndexPath: This method is called when the table view needs a cell.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
   
         // Configure the cell.
         cell.textLabel.text = [self.datas objectAtIndex:indexPath.row];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         return cell;
}


- Finally release the the values

- (void)dealloc {
    [super dealloc];
         [datas release];
}

No comments:

Post a Comment