uitableview Getting started with uitableview UITableView in detail

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

What is UITableView?

UITableView is a most frequently used user interface object that presents data in a scrollable list of multiple rows in a single column that can also be divided into sections.It allows vertical scrolling only and is a subclass of UIScrollView.

Why do we use UITableView?

We can use UITableView to present a list of options that can be selected, to navigate through hierarchically structured data, present an indexed list of items, to display detail information and controls in visually distinct groupings making use of sections.

We can see use of UITableView in our contacts, mailing lists etc. It is not only just used to present textual data but also images along with texts can be listed such as in YouTube app.

Detailed instructions on getting UITableView set up or installed using Story Board.

  1. Create a simple project for Single View application.
  2. In the Object Library, select the “Table View” object and drag it into the view of your view controller. Simply running the project you will see a blank page with lines.
  3. Now if you simply want a scrollable view with contents, then drag a UIView into the UITableView, adjust its size and drag rest of the UIElements into that view as per requirements. But if you want to present a list of similar format, we use UITableViewCell.
  4. The UITableViewCell class defines the attributes and behavior of the cells that appear in UITableView objects. This class includes properties and methods for setting and managing cell content and background (including text, images, and custom views), managing the cell selection and highlight state, managing accessory views, and initiating the editing of the cell contents.
  5. The best part of using UITableViewCell is reusability. The purpose of dequeueReusableCellWithIdentifier is to use less memory.For e.g. if you have a list of 1000 entries and only 10 entries are visible at a time then only the visible cells are allocated in memory, rest are reused as when the user scrolls the list. All you need to do is drag a UITableViewCell and drop to tableView. Then click on cell-> Go to attribute inspector-> Set tableView style to custom and identifier to anything unique you would like say myCell.
  6. Now we need to conform to data source so that object gives data to another object. For example, the UITableViewDataSource protocol has methods such as cellForRowAtIndexPath and numberOfRowsInSection dictating what should be displayed in the table. Whereas delegate type object responds to actions that another object takes. For example, the UITableViewDelegate protocol has methods such as didSelectRowAtIndexPath for performing actions upon a user selecting a particular row in a table. 7.When you conform to datasource you need to implement its required method i.e
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
        // Here you need to give the total number of items you want to list. For example if you want list of 2 numbers, then:

           return 2; 

 }

and

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

//Here you need to dequeue the reusable cell which we discussed in point 5. Then you can modify your cell here according to you and customize it here as per your requirement. Here the call comes for numberOfRows number of times. 

static NSString *cellIdentifier = @"cellID";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:
    UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
} 
if(indexPath.row){
    [cell.textLabel setText:"1"];
}else{
    [cell.textLabel setText:"2"];
}

return cell;
}
  1. Also about NSIndexPath:- The NSIndexPath class represents the path to a specific node in a tree of nested array collections. This path is known as an index path. Its objects are always of length 2. They're used for example to index a table view cell. The first index in a NSIndexPath object is called the section, the second is the row. An index path object with section 0 and row 0 indicates the first row in the first section. Use [NSIndexPath indexPathForRow:inSection:] to quickly create an index path.


Got any uitableview Question?