Table of contents
1.
Introduction
2.
Use of TableView
3.
Important TableView Properties
4.
Adding a Table View
4.1.
Steps
5.
TableView Delegate Methods
6.
TableView DataSource Methods
7.
Frequently Asked Questions
7.1.
What is a TableView?
7.2.
What is TableView in Swift?
7.3.
What does TableView Register do?
7.4.
How do you make a TableView?
7.5.
What is TableView dataSource?
8.
Conclusion
Last Updated: Mar 27, 2024

iOS TableView

Author GAZAL ARORA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

  1. 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.

tableView.delegate = self  
tableView.datasource = self  

TableView Delegate Methods

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.
 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!
 

Live masterclass