Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
TableView is a view that allows users to arrange data using rows in a single column. It can be found in almost every iOS app. Applications like Facebook, Instagram, Contacts, Settings, etc., also use tableView view.
The Tableview displays data in the settings app, as shown in the image below.
The tableview is an instance of the UITableView class inherited from the UIScrollView class.
class UITableView : UIScrollView
Use of TableView
We use Tableview in iOS applications whenever we need to display a single column with vertically scrolling content. Multiple records (split into rows) can be displayed in the Tableview, which can be scrolled vertically. Each data source record is displayed in each row of the tableview. For example, in the contact app, we show each contact name in its own Tableview row, and when we click on that row, we get the contact's details.
It has distinct features such as headers, footers, rows, and sections.
Important TableView Properties
delegate
rowHeight
dataSource
sectionHeaderHeight
separatorColor
sectionFooterHeight
tableFooterView
tableHeaderView
Adding a Table View
Drag a table view controller (UITableViewController) object to your storyboard to add a table view to your interface. Xcode creates a new scene with both the view controller and a table view that you can modify and use.
Table views are data-driven, with data often coming from a data source object that you give. The data source object is in charge of managing your app's data and of creating and setting the table's cells. You can configure the content of your table in your storyboard file instead if the content of your table never changes.
Steps
To add the Tableview to the storyboard, go to the object library, search for Tableview, and then drag the result to the storyboard.
2. Set its delegate and data source properties
The data source object contains the data received by an API call from the database server in real-world applications.
The ViewController's viewDidLoad method uses the following line of code to set the tableview's delegate and data source attributes.
The Tableview delegate methods are defined to provide the Tableview with the following functionalities
For the Tableview sections, we can define custom headers and footers.
Rows, headers, and footers can have specific heights set.
Estimate the heights of the rows, headers, and footers.
We can define a method to deal with row selections.
TableView DataSource Methods
We need a DataSource object that implements the UITableViewDataSource protocol to keep track of the data that the Tableview will present. The Tableview data is managed by the datasource object. The basic functions of the datasource object are as follows.
It specifies how many rows and sections will be shown in the Tableview.
For each row in the Tableview, it allocates the reused cells.
In the Tableview sections, it offers titles for headers and footers.
Frequently Asked Questions
What is a TableView?
A table view shows a single column of content that scrolls vertically and is separated into rows and sections. A table's each row shows a single piece of information about your app.
What is TableView in Swift?
The UITableView class, a subclass of UIScrollView, is used to create table views. The table view cells are the repeated rows or views displayed in the table view. A table view cell is an instance of the UITableViewCell class, which is frequently subclassed to make custom table view cells.
What does TableView Register do?
Returns a reusable table-view cell object and adds it to the table for the provided reuse identifier.
How do you make a TableView?
Step 1: Using Interface Builder, create a UITableView.
Step2: Create an outlet for the UITableView
Step 3: Adhere to the UITableViewDataSource standard.
Step 4: Add DataSource and Register UITableViewCell.
Step 5: Update the implementations of the UITableViewDataSource function.
What is TableView dataSource?
Table views are only responsible for how their data is shown; they are not in charge of the data itself. You provide the table with a data source object—an object that implements the UITableViewDataSource protocol to manage the data. A data source object responds to table-related data requests.
Conclusion
In this article, we looked at the TableView view in iOS. It can be found in almost every iOS app—for example, Facebook, Settings, Contacts, Instagram, etc.