How To Create TableView and Implementation Of It’s Delegate Method In Objective c

Simulator Screen Shot - iPhone 7 - 2017-10-11 at 15.45.00

 

Jump To :

1)Create Xcode Project 

2)UITableViewDelegate Method

3)Design UI

4)Attach UITableView Datasource & Delegate

5)Drag TableViewCell On UITableView

6)Create new file for TableViewCell

7)Drag Label And UIImageView In Cell

8) Implementation Of Delegate And DataSource

9)Array Initialigation

10)UITableviewDataSouce Methods

11)UITableviewDelegate Methods

Objective : The Main Purpose Of  This Project Is To Display List Of Data In TableView And Implementation Of  It’s Delegate Method .

Introduction

UITableview Is Important Content In UIKit Framework  Basically It Is Responsible For Displaying Small And Large List  Of Data  Of Any Object .

The Protocol  Of  TableView Is UITableViewDelegate And UITableViewDataSource .

UITableViewDelegate Is Responsible For Event Handling And UITableViewDataSource Is Responsible For Presenting And Displaying Data As a List Of Rows In TableView.

Step 1  Create XCode project

Create Xcode Project .This project will Contain only on view controller which will our main view controller  .

Step 2  UITableViewDelegate Method

UITableViewDelegate Is Responsible For Event Handling.When UITableView detects any touches from user, it will notify its delegate, which will respond to touch event.

Step 3 Design UI

To UITableview , First drag Tableview on View of  Viewcontroller

create IBOutlet Of tableview in ViewController.h
Screen Shot 2017-10-11 at 1.01.51 PM

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITableView *tableview;

@end

Step 4  Attach UITableView Datasource & Delegate

Connect you Tableview to UITableViewDataSource and UItableViewDelegate.Screen Shot 2017-10-11 at 1.06.53 PM

select Tableview  , open the Connection Inspector, and drag from the data source outlet (circle on its right) to the main view controller. Same Step for delegate of  UItableView.

Step 5  Drag TableViewCell On UITableView

For design custom cell , need to drag TableViewCellScreen Shot 2017-10-11 at 1.12.43 PM

 

Step 6   Create new file for TableViewCell

Create new file with  Super UItableViewCell  with name TableViewCell(you can also give name file name like table_cell,first_cell etc..)

Note: don’t create Xib file if using storyboard 

Screen Shot 2017-10-11 at 1.35.16 PM

next, Select TableViewCell which is on ViewController    and assign class   TableViewCell(your created file name)

Screen Shot 2017-10-11 at 1.46.59 PM

also give identifier to cell  like (cellIndentifire,cellID  etc..)

Screen Shot 2017-10-11 at 2.29.39 PM

Step 7  Drag Label And UIImageView In Cell

Drag label and UIImageView In TableViewCell Screen Shot 2017-10-11 at 1.53.39 PM

Create IBOutlet of label and image view  in TableViewCell.h 

Screen Shot 2017-10-11 at 1.57.35 PM

Step 8   Implementation Of Delegate And DataSource

To conform UItableViewDelegate And UITableViewDataSource for UItableView

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@end

Step 9    Array Initialigation

now create two array  in  ViewController.h   one for list of countryName  and another for list of countryImage .

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property(strong,nonatomic)NSMutableArray *countryNameData;

@property(strong,nonatomic)NSMutableArray *countryImageData;

@end

next , initialize in ViewDidLoad  of  ViewController.m

@implementation ViewController

– (void)viewDidLoad {

[super viewDidLoad];

_countryNameData = [ NSMutableArray arrayWithObjects:@”India”,@”Rusia”,@”America”,@”Israil”,@”Argentina”,@”France”, nil];

_countryImageData = [NSMutableArray arrayWithObjects:@”india”,@”rusia” ,@”america”,@”israil”,@”argentina”,@”france”,nil];

// Do any additional setup after loading the view, typically from a nib.

}

Step 10    UITableviewDataSouce Methods

import TableViewCell.h l in ViewController.h.  needed,  because  we have to show cell content on tableview and the cell class is  TableViewCell.

#import <UIKit/UIKit.h>

#import “TableViewCell.h”

Implement following UITableView datasource methods in your main viewcontroller.m  file.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [_countryNameData count];

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”cellID”;

TableViewCell *cell = [_tableview dequeueReusableCellWithIdentifier:CellIdentifier ];

cell.CountryName.text = [_countryNameData objectAtIndex:indexPath.row];

cell.countryImage.image = [UIImage imageNamed:[_countryImageData objectAtIndex:indexPath.row]];

return cell;

}

Step 11    UITableviewDelegate Methods

Most Common method  of  UITableviewDelegate  Protocol is

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

}

this method is use for selection of specific Cell

Implement following UITableView delegate methods in your main viewcontroller.m  file.

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@”CountryName”

message:[NSString stringWithFormat:@”You Selected,%@”,[_countryNameData objectAtIndex:indexPath.row]]

preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@”OK” style:UIAlertActionStyleDefault

handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];

[self presentViewController:alert animated:YES completion:nil];

}

Screenshots:                                                                            

     Simulator Screen Shot - iPhone 7 - 2017-10-11 at 15.45.00          Simulator Screen Shot - iPhone 7 - 2017-10-11 at 15.46.27

NOTE: From here you can download the source code

I hope this will help you to understand that how to implement the Tableview In Objective -c

To download the source code please click on the link :TableViewAndIts’sDelegateMethod

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *