An instance of UITableView (or simply, a table view) is a means for displaying and editing hierarchical lists of information.
A table view displays a list of items in a single column. UITableView is a subclass of UIScrollView, which allows users to scroll through the table, although UITableView allows vertical scrolling only. The cells comprising the individual items of the table are UITableViewCell objects; UITableView uses these objects to draw the visible rows of the table. Read More

To create a Simple Table View.

  1.  Create a new Xcode Project for (Single View Application).
  2. . Give Project a name ( Lets say SimpleTableView).
  3. Click next button and the Project will be created,
  4. Now go to Main StoryBoard of the Project and In the Object Library, select the “Table View” object and drag it into the view.
  5. Connect the dataSource and the delegate to the View Controller.
  6.  Create a IBoutlet for the tableview in the ViewContoller.h.

After Connectimg the IBOutlet Your View Controller will Look something Like this.

//
// ViewController.h
// SimpleTableView
//
// Created by Nilesh on 4/13/16.
// Copyright © 2016 Nilesh. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

 

 
  1. Now go to Your View controller.m  and declare a array under the interface Section.

@interface ViewController (){
NSArray *tableDataArray;
}

 

  1. Go to View did load and initialize the array with few Elements.

– (void)viewDidLoad {
[super viewDidLoad];
tableDataArray = [NSArray arrayWithObjects:@”Ram”,@”Shyam”,@”Mohan”,@”Nilesh”, nil];
// Do any additional setup after loading the view, typically from a nib.
}

 

9. Finally, we have to add two datasource methods: “tableView:numberOfRowsInSection” and “tableView:cellForRowAtIndexPath”. These two methods are part of the UITableViewDataSource protocol. It’s mandatory to implement the methods when configuring a UITableView. The first method is used to inform the table view how many rows are in the section. So let’s add the below code. The “count” method simply returns the number of items in the “tableDataArray” array.

# pragma – mark table view DataSource Methods

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

{

    return [tableDataArray count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *simpleTableIdentifier = @”SimpleTableItem”;

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }

    

    cell.textLabel.text = [tableDataArray objectAtIndex:indexPath.row];

    return cell;

}

 

  1. So Finally Your ViewController.m will Look something Like this.

 

// ViewController.m
// SimpleTableView
//
// Created by Nilesh on 4/13/16.
// Copyright © 2016 Nilesh. All rights reserved.
//

#import “ViewController.h”

@interface ViewController (){
NSArray *tableDataArray;
}

@end

@implementation ViewController

– (void)viewDidLoad {
[super viewDidLoad];
tableDataArray = [NSArray arrayWithObjects:@”Ram”,@”Shyam”,@”Mohan”,@”nilesh”, nil];
// Do any additional setup after loading the view, typically from a nib.
}

– (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

# pragma – mark table view DataSource Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableDataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @”SimpleTableItem”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [tableDataArray objectAtIndex:indexPath.row];
return cell;
}

#pragma mark – TableView delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{

Nslog(@”The value of indexpathrow is %@”,indexPath.row);

}

@end

Enjoy Coding 🙂

Advertisements