UITableView is one of the most useful and commonly used view components in iOS development. It helps to display data in a scrollable list, such as a news feed, a reminders list, or any table of items.
UITableView contains UITableViewCells in it. To understand the relation between the two, think about an array and items. You can easily map items of an array to the cell in a table view. Additionally, you can create sections to map multiple arrays or a 2D array.
Below is an example of UITableView usage. The native Settings app uses a table view with sections. The first section has 3 cells, whereas the second section has 1 cell.
Usually, cells used in a table view have the same design and size, decreasing the overhead of redrawing the cells. However, Apple allows you to create any variety of cells for one table view.
Open the Xcode app on your Mac to create a new project. Select the new App project from the popup and provide a nice title for the Product Name. For our example project, we are calling it ColorList. Your interface should be Storyboard and language set to Swift.
Question: Why Storyboard? For ‘Interface’ we are using Storyboard, but if you are curious, the other option is SwiftUI. SwiftUI is the latest advancement to design UIs, but many developers believe it is not yet fully production-ready as iOS 14.
Lastly, you can go the old way and create the whole UI in code as well. But I like to keep the view stuff separate instead of one fat controller.
Each new app comes with one view controller and a Storyboard. In simple words, every screen of the app is a UIViewController in code.
That's it, your scrollable table view UI is ready to roll. You can add the constraints to make it stretch 100% in width and height.
The next step is to create the outlets in the view controller class. That way we can have access to table view in our controller to further add business and UI logic. There are multiple ways to do so. Here is the quickest one:
The table view has two weak objects inside the UITableView class to populate the data and manage delegation. Weak means the objects are not retained strongly.
The dataSource object is responsible for providing cells and their data for a table view. It is of ‘UITableViewDataSource?’ type.
The delegate object is of ‘UITableViewDelegate?’ type. It handles selection, deletion, header/footer, and other related actions.
To start,
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//TODO: Return items count
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//TODO: Return cell
return UITableViewCell()
}
let rainbow: [UIColor] = [.red, .yellow, .green, .orange, .blue, .purple, .magenta]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rainbow.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell", for: indexPath)
cell.backgroundColor = rainbow[indexPath.item]
return cell
}
UITableView is your go-to UI view in most of the cases where scrolling behavior is desired. UITableView inherits from UIScrollView, just like UICollectionView - a UITableView like view where you can have cells both ways, i.e., horizontal and vertical.
The cell we are using is the default table view cell provided by iOS. You can create a custom class and, like the above steps, connect the UI and class. We will upload a How-To guide next week to create a custom-designed UITableViewCell.
The default cell can provide a title, background, and accessory view. We will add a title using defaultConfiguration- like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell", for: indexPath)
cell.backgroundColor = rainbow[indexPath.item]
//Default Content Configuration
var content = cell.defaultContentConfiguration()
content.text = rainbow[indexPath.item].accessibilityName.capitalized
cell.contentConfiguration = content
return cell
}
That is what it will look like:
You can create different design cells in the same table view. By assigning different identifiers you can dequeue your required cell. The dequeueReusableCell method improves performance by creating only visible cells on the screen and a few more. By efficiently caching cells it can provide a fluid user experience.
That's how your whole code should look like. You can download the sample source code from here.
Let us know what you built with UITableView in our Hackernoon Community.