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.
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
.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.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.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;
}
[NSIndexPath indexPathForRow:inSection:]
to quickly create an index path.