custom cell in objective c

24
Objective C HOW TO CREATE CUSTOM CELL IN IPHONE.

Upload: vishal-verma

Post on 15-Jan-2015

417 views

Category:

Technology


1 download

DESCRIPTION

HOW TO CREATE CUSTOM CELL IN IPHONE.

TRANSCRIPT

Page 1: Custom cell in objective c

Objective C

HOW TO CREATE CUSTOM CELL IN IPHONE.

Page 2: Custom cell in objective c

Open your Xcode and Create a new empty project

Page 3: Custom cell in objective c

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

Page 4: Custom cell in objective c
Page 5: Custom cell in objective c
Page 6: Custom cell in objective c

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;}

Page 7: Custom cell in objective c

Adopt UITableViewDataSource Protocol in UserNameListViewController.m file

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

Page 8: Custom cell in objective c

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

Page 9: Custom cell in objective c

Declare a function into UserNameListViewController.m

- (void)classInitialization;

Page 10: Custom cell in objective c

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.}

Page 11: Custom cell in objective c

Implement classInitialization function.

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

Page 12: Custom cell in objective c

Now create a new cell file

Page 13: Custom cell in objective c

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

Page 14: Custom cell in objective c

Synthesize in NameListCell.m file.

• @synthesize userNameLabel;

Page 15: Custom cell in objective c

Create xib file for custom cell.

Page 16: Custom cell in objective c
Page 17: Custom cell in objective c
Page 18: Custom cell in objective c
Page 19: Custom cell in objective c

Make connection on NameListCell.xib

Page 20: Custom cell in objective c
Page 21: Custom cell in objective c

Add TableView on UserNameListViewController.xib

Page 22: Custom cell in objective c

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;}

Page 23: Custom cell in objective c

Now save and Run.

Page 24: Custom cell in objective c

Thank You For Reading My Blog.