custom cell in objective c

Post on 15-Jan-2015

417 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

HOW TO CREATE CUSTOM CELL IN IPHONE.

TRANSCRIPT

Objective C

HOW TO CREATE CUSTOM CELL IN IPHONE.

Open your Xcode and Create a new empty project

Create a new class using right click on AppDelegate.m file

Load your first class into AppDelegate.m file.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. // Make object of that class and load nib file. UserNameListViewController *userNameListViewController = [[UserNameListViewController alloc]initWithNibName:@"UserNameListViewController" bundle:nil]; self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController=userNameListViewController; [self.window makeKeyAndVisible]; return YES;}

Adopt UITableViewDataSource Protocol in UserNameListViewController.m file

@interface UserNameListViewController ()<UITableViewDataSource/*Adopt tableViewDataSource Protocol */>{ } @end

Create an array for load data into TableView.

@interface UserNameListViewController ()<UITableViewDataSource/*Adopt tableViewDataSource Protocol */>{ NSArray *userNameArray; // Create an array for load data into tableView;}@end

Declare a function into UserNameListViewController.m

- (void)classInitialization;

Call classInitialization from ViewDidLoad function.

- (void)viewDidLoad{ [super viewDidLoad]; [self classInitialization]; // Call from view did load for class Initialization. // Do any additional setup after loading the view from its nib.}

Implement classInitialization function.

#pragma mark - Methods Implementaion. - (void)classInitialization{// Initialization array with user names. userNameArray = [[NSArray alloc]initWithObjects:@"Vishal Verma",@"Ankit",@"Parag",@"kalpak", nil];}

Now create a new cell file

Make a label object in NameListCell.h file for display user’s name on custom cell;

#import <UIKit/UIKit.h> @interface NameListCell : UITableViewCell{ IBOutlet UILabel *userNameLabel;}@property(nonatomic,retain)IBOutlet UILabel *userNameLabel;@end

Synthesize in NameListCell.m file.

• @synthesize userNameLabel;

Create xib file for custom cell.

Make connection on NameListCell.xib

Add TableView on UserNameListViewController.xib

Implement TableView Datasource Methods into

#pragma mark - TableView Datasource Methods. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return userNameArray.count;} - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"cellIdentifier"; // Make Object Of Custom Class. NameListCell *nameListCell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if(!nameListCell){ // Load Nib File Into Array. NSArray *loadCellArray = [[NSBundle mainBundle]loadNibNamed:@"NameListCell" owner:self options:nil]; nameListCell = [loadCellArray objectAtIndex:0]; } nameListCell.userNameLabel.text = [userNameArray objectAtIndex:[indexPath row]]; return nameListCell;}

Now save and Run.

Thank You For Reading My Blog.

top related