Sunday, 25 October 2015

Know about UITableView in Objective c

In this post , I will tell you about UITableView in iOS development.
 TableView is one of the most common UI (User Interface) elements in iOS apps. Tableview is used to display list of data in the form of text or images . The best example of TableViews is music player application in iPhone .


Getting started with the code

Open Xcode , start a New project and name it UITableView Demo and save it to your desired location.
Now in your Main.storyboard . Select the size of the simulator for designing . (Like i have choose 4 inch )


Select View Controller Size











Now , in the bottom of the utility area , write UITableView and Drag to the ViewController.


Drag ViewController
















Now , before proceeding any further , run the simulator by cmd + r or windows + r. You will see a blank tableview.

.
Simulator


Now lets add some data to the tableview . 

First thing we need to do is to add UITableViews`s datasource and delegate methods.



What are datasource and delegates ?


Datasource : - Datasource is used to display data in our tableview. It is a link between our data and tableview. Without using datasource , we can`t see any data in the UITableView.

Two necessary datasource methods are :

1) tableView:numberOfRowsInSection >> It tells the tableview about the number of rows in tableView.
2) tableView:cellForRowAtIndexPath >> It tells about the data in each row.


 Delegate : Delegates provides functionality to UITableViews . Suppose you need to click on a row of the table . So at that time , delegates will be used .

For displaying data , first add tableview datasource and delegates to viewController.h


@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>


Now, for displaying data in our tableView , we will use NSArrays .

So add NSArray in your .h file .  @property NSArray *items;


Now, your .h file (ViewController) will look like this:



#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property NSArray *items;

@end




Now in .m file initialize the data in array

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _items = @[@"iMac",@"iPhone",@"iPad",@"iPod"];
}

To know more about Arrays , view my previous blogs.

Now, add TableView two neccessary datasource methods .

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_items count]; // number of rows
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

 // It asks the table view if it has any available cells already created which tableview is not using . so that it can reuse them.
// The identifier is there to differentiate between different types of cells.


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

// If the cell is nil it means no cell was available for reuse and that we should create a new one


    cell.textLabel.text = [_items objectAtIndex:indexPath.row]; // adding text to rows
    return cell;

}


After that, connect datasource and delegates to main.storyboard by clicking on main.storyboard . Then click on view controller .
Then Right click and drag it on the yellow button above the viewcontroller, connect it to the view  .






After adding datasource and delegate methods , you can see them in the utility area`s last button.














Now , Build and run app by cmd + r . You will see your tableview with list of data in it.

















Thank you :)



No comments:

Post a Comment