Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Custom cell in objective c

1,039 views

Published on

HOW TO CREATE CUSTOM CELL IN IPHONE.

Published in: Technology, Education
  • Be the first to comment

Custom cell in objective c

  1. 1. Objective C HOW TO CREATE CUSTOM CELL IN IPHONE.
  2. 2. Open your Xcode and Create a new empty project
  3. 3. Create a new class using right click on AppDelegate.m file
  4. 4. 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; }
  5. 5. Adopt UITableViewDataSource Protocol in UserNameListViewController.m file @interface UserNameListViewController ()<UITableViewDataSource/*Adopt tableViewDataSource Protocol */>{ } @end
  6. 6. 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
  7. 7. Declare a function into UserNameListViewController.m - (void)classInitialization;
  8. 8. 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. }
  9. 9. Implement classInitialization function. #pragma mark - Methods Implementaion. - (void)classInitialization{ // Initialization array with user names. userNameArray = [[NSArray alloc]initWithObjects:@"Vishal Verma",@"Ankit",@"Parag",@"kalpak", nil]; }
  10. 10. Now create a new cell file
  11. 11. 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
  12. 12. Synthesize in NameListCell.m file. • @synthesize userNameLabel;
  13. 13. Create xib file for custom cell.
  14. 14. Make connection on NameListCell.xib
  15. 15. Add TableView on UserNameListViewController.xib
  16. 16. 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; }
  17. 17. Now save and Run.
  18. 18. Thank You For Reading My Blog.

×