Tuesday, June 25, 2024
HomeIOS DevelopmentUITableView tutorial in Swift - The.Swift.Dev.

UITableView tutorial in Swift – The.Swift.Dev.


Find out how to create a desk view programmatically?

Let’s soar straight into the coding half, however first: begin Xcode, create a brand new iOS single view app challenge, enter some title & particulars for the challenge as standard, use Swift and at last open the ViewController.swift file straight away. Now seize your keyboard! ⌨️

Professional tip: use Cmd+Shift+O to rapidly soar between information

I am not going to make use of interface builder on this tutorial, so how can we create views programmatically? There’s a methodology known as loadView that is the place it is best to add customized views to your view hierarchy. You may possibility+click on the tactic title in Xcode & learn the dialogue about loadView methodology, however let me summarize the entire thing.

We’ll use a weak property to carry a reference to our desk view. Subsequent, we override the loadView methodology & name tremendous, with the intention to load the controller’s self.view property with a view object (from a nib or a storyboard file if there may be one for the controller). After that we assign our model new view to an area property, flip off system supplied format stuff, and insert our desk view into our view hierarchy. Lastly we create some actual constraints utilizing anchors & save our pointer to our weak property. Straightforward! πŸ€ͺ

class ViewController: UIViewController {

    weak var tableView: UITableView!

    override func loadView() {
        tremendous.loadView()

        let tableView = UITableView(body: .zero, model: .plain)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        NSLayoutConstraint.activate([
        self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: tableView.topAnchor),
            self.view.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
            self.view.leadingAnchor.constraint(equalTo: tableView.leadingAnchor),
            self.view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
        ])
        self.tableView = tableView
    }
}

At all times use auto format anchors to specify view constraints, if you do not know the way to use them, test my format anchors tutorial, it is takes solely about quarter-hour to be taught this API, and you will not remorse it. It is an especially great tool for any iOS developer! πŸ˜‰

You may ask: ought to I take advantage of weak or robust properties for view references? I might say in a lot of the instances in case you are not overriding self.view it is best to use weak! The view hierarchy will maintain your customized view via a powerful reference, so there isn’t a want for silly retain cycles & reminiscence leaks. Belief me! πŸ€₯

UITableViewDataSource fundamentals

Okay, we’ve an empty desk view, let’s show some cells! So as to fill our desk view with actual information, we’ve to adapt to the UITableViewDataSource protocol. By means of a easy delegate sample, we will present numerous data for the UITableView class, so it will to understand how a lot sections and rows can be wanted, what sort of cells must be displayed for every row, and lots of extra little particulars.

One other factor is that UITableView is a very environment friendly class. It will reuse all of the cells which might be at the moment not displayed on the display, so it will eat approach much less reminiscence than a UIScrollView, if you need to cope with a whole lot or 1000’s of things. To assist this conduct we’ve to register our cell class with a reuse identifier, so the underlying system will know what sort of cell is required for a particular place. βš™οΈ

class ViewController: UIViewController {

    var gadgets: [String] = [
        "πŸ‘½", "🐱", "πŸ”", "🐢", "🦊", "🐡", "🐼", "🐷", "πŸ’©", "🐰",
        "πŸ€–", "πŸ¦„", "🐻", "🐲", "🦁", "πŸ’€", "🐨", "🐯", "πŸ‘»", "πŸ¦–",
    ]

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")

        self.tableView.dataSource = self
    }
}

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return self.gadgets.depend
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        let merchandise = self.gadgets[indexPath.item]
        cell.textLabel?.textual content = merchandise
        return cell
    }
}

After including a number of strains of code to our view controller file, the desk view is now in a position to show a pleasant listing of emojis! We’re utilizing the built-in UITableViewCell class from UIKit, which comes actually useful in case you are good to go together with the “iOS-system-like” cell designs. We additionally conformed to the info supply protocol, by telling what number of gadgets are in our part (at the moment there is just one part), and we configured our cell contained in the well-known cell for row at indexPath delegate methodology. 😎

Customizing desk view cells

UITableViewCell can present some primary parts to show information (title, element, picture in several types), however normally you will want customized cells. Here’s a primary template of a customized cell subclass, I will clarify all of the strategies after the code.

class MyCell: UITableViewCell {

    override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(model: model, reuseIdentifier: reuseIdentifier)

        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)

        self.initialize()
    }

    func initialize() {

    }
    
    override func prepareForReuse() {
        tremendous.prepareForReuse()

    }
}

The init(model:reuseIdentifier) methodology is a superb place to override the cell model property if you will use the default UITableViewCell programmatically, however with totally different types (there isn’t a choice to set cellStyle after the cell was initialized). For instance in the event you want a .value1 styled cell, simply go the argument on to the tremendous name. This manner you’ll be able to profit from the 4 predefined cell types.

You will additionally should implement init(coder:), so it is best to create a typical initialize() operate the place you can add your customized views to the view hierarchy, like we did within the loadView methodology above. In case you are utilizing xib information & IB, you should use the awakeFromNib methodology so as to add additional model to your views via the usual @IBOutlet properties (or add additional views to the hierarchy as effectively). πŸ‘

The final methodology that we’ve to speak about is prepareForReuse. As I discussed earlier than cells are being reused so if you wish to reset some properties, just like the background of a cell, you are able to do it right here. This methodology can be known as earlier than the cell goes to be reused.

Let’s make two new cell subclasses to mess around with.

class DetailCell: UITableViewCell {

    override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(model: .subtitle, reuseIdentifier: reuseIdentifier)

        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)

        self.initialize()
    }

    func initialize() {
        
    }

    override func prepareForReuse() {
        tremendous.prepareForReuse()

        self.textLabel?.textual content = nil
        self.detailTextLabel?.textual content = nil
        self.imageView?.picture = nil
    }
}

Our customized cell could have a giant picture background plus a title label within the middle of the view with a customized sized system font. Additionally I’ve added the Swift brand as an asset to the challenge, so we will have a pleasant demo picture. πŸ–Ό

class CustomCell: UITableViewCell {

    weak var coverView: UIImageView!
    weak var titleLabel: UILabel!

    override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(model: model, reuseIdentifier: reuseIdentifier)

        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)

        self.initialize()
    }

    func initialize() {
        let coverView = UIImageView(body: .zero)
        coverView.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(coverView)
        self.coverView = coverView

        let titleLabel = UILabel(body: .zero)
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(titleLabel)
        self.titleLabel = titleLabel

        NSLayoutConstraint.activate([
            self.contentView.topAnchor.constraint(equalTo: self.coverView.topAnchor),
            self.contentView.bottomAnchor.constraint(equalTo: self.coverView.bottomAnchor),
            self.contentView.leadingAnchor.constraint(equalTo: self.coverView.leadingAnchor),
            self.contentView.trailingAnchor.constraint(equalTo: self.coverView.trailingAnchor),

            self.contentView.centerXAnchor.constraint(equalTo: self.titleLabel.centerXAnchor),
            self.contentView.centerYAnchor.constraint(equalTo: self.titleLabel.centerYAnchor),
        ])

        self.titleLabel.font = UIFont.systemFont(ofSize: 64)
    }

    override func prepareForReuse() {
        tremendous.prepareForReuse()

        self.coverView.picture = nil
    }
}

That is it, let’s begin utilizing these new cells. I will even inform you the way to set customized top for a given cell, and the way to deal with cell choice correctly, however first we have to get to know with one other delegate protocol. 🀝

Primary UITableViewDelegate tutorial

This delegate is accountable for plenty of issues, however for now we’ll cowl only a few fascinating elements, like the way to deal with cell choice & present a customized cell top for every gadgets contained in the desk. Here’s a fast pattern code.

class ViewController: UIViewController {

    override func viewDidLoad() {
            tremendous.viewDidLoad()

            self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
            self.tableView.register(DetailCell.self, forCellReuseIdentifier: "DetailCell")
            self.tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")

            self.tableView.dataSource = self
            self.tableView.delegate = self
    }
}
extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
        let merchandise = self.gadgets[indexPath.item]
        cell.titleLabel.textual content = merchandise
        cell.coverView.picture = UIImage(named: "Swift")
        return cell
    }
}

extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 128
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let merchandise = self.gadgets[indexPath.item]

        let alertController = UIAlertController(title: merchandise, message: "is in da home!", preferredStyle: .alert)
        let motion = UIAlertAction(title: "Okay", model: .default) { _ in }
        alertController.addAction(motion)
        self.current(alertController, animated: true, completion: nil)
    }
}

As you’ll be able to see I am registering my model new customized cell lessons within the viewDidLoad methodology. I additionally modified the code contained in the cellForRowAt indexPath methodology, so we will use the CustomCell class as an alternative of UITableViewCells. Do not be afraid of pressure casting right here, if one thing goes improper at this level, your app ought to crash. πŸ™ƒ

There are two delegate strategies that we’re utilizing right here. Within the first one, we’ve to return a quantity and the system will use that top for the cells. If you wish to use totally different cell top per row, you’ll be able to obtain that too by checking indexPath property or something like that. The second is the handler for the choice. If somebody faucets on a cell, this methodology can be known as & you’ll be able to carry out some motion.

An indexPath has two fascinating properties: part & merchandise (=row)

A number of sections with headers and footers

It is doable to have a number of sections contained in the desk view, I will not go an excessive amount of into the small print, as a result of it is fairly simple. You simply have to make use of indexPaths with the intention to get / set / return the correct information for every part & cell.

import UIKit

class ViewController: UIViewController {

    weak var tableView: UITableView!

    var placeholderView = UIView(body: .zero)
    var isPullingDown = false

    enum Model {
        case `default`
        case subtitle
        case customized
    }

    var model = Model.default

    var gadgets: [String: [String]] = [
        "Originals": ["πŸ‘½", "🐱", "πŸ”", "🐢", "🦊", "🐡", "🐼", "🐷", "πŸ’©", "🐰","πŸ€–", "πŸ¦„"],
        "iOS 11.3": ["🐻", "🐲", "🦁", "πŸ’€"],
        "iOS 12": ["🐨", "🐯", "πŸ‘»", "πŸ¦–"],
    ]

    override func loadView() {
        tremendous.loadView()

        let tableView = UITableView(body: .zero, model: .plain)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        NSLayoutConstraint.activate([
            self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: tableView.topAnchor),
            self.view.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: tableView.bottomAnchor),
            self.view.leadingAnchor.constraint(equalTo: tableView.leadingAnchor),
            self.view.trailingAnchor.constraint(equalTo: tableView.trailingAnchor),
        ])
        self.tableView = tableView
    }

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        self.tableView.register(DetailCell.self, forCellReuseIdentifier: "DetailCell")
        self.tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")

        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.separatorStyle = .singleLine
        self.tableView.separatorColor = .lightGray
        self.tableView.separatorInset = .zero

        self.navigationItem.rightBarButtonItem = .init(barButtonSystemItem: .refresh, goal: self, motion: #selector(self.toggleCells))
    }

    @objc func toggleCells() {

        change self.model {
        case .default:
            self.model = .subtitle
        case .subtitle:
            self.model = .customized
        case .customized:
            self.model = .default
        }

        DispatchQueue.major.async {
            self.tableView.reloadData()
        }
    }

    

    func key(for part: Int) -> String {
        let keys = Array(self.gadgets.keys).sorted { first, final -> Bool in
            if first == "Originals" {
                return true
            }
            return first < final
        }
        let key = keys[section]
        return key
    }

    func gadgets(in part: Int) -> [String] {
        let key = self.key(for: part)
        return self.gadgets[key]!
    }

    func merchandise(at indexPath: IndexPath) -> String {
        let gadgets = self.gadgets(in: indexPath.part)
        return gadgets[indexPath.item]
    }
}

extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.gadgets.keys.depend
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return self.gadgets(in: part).depend
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let merchandise = self.merchandise(at: indexPath)
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.titleLabel.textual content = merchandise
        cell.coverView.picture = UIImage(named: "Swift")
        return cell
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection part: Int) -> String? {
        return self.key(for: part)
    }

}

extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 128
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        let merchandise = self.merchandise(at: indexPath)
        let alertController = UIAlertController(title: merchandise, message: "is in da home!", preferredStyle: .alert)
        let motion = UIAlertAction(title: "Okay", model: .default) { _ in }
        alertController.addAction(motion)
        self.current(alertController, animated: true, completion: nil)
    }
}

Though there may be one fascinating addition within the code snippet above. You may have a customized title for each part, you simply have so as to add the titleForHeaderInSection information supply methodology. Yep, it seems like shit, however this one just isn’t about excellent UIs. πŸ˜‚

Nonetheless in case you are not glad with the format of the part titles, you’ll be able to create a customized class & use that as an alternative of the built-in ones. Right here is the way to do a customized part header view. Right here is the implementation of the reusable view:

class HeaderView: UITableViewHeaderFooterView {

    weak var titleLabel: UILabel!

    override init(reuseIdentifier: String?) {
        tremendous.init(reuseIdentifier: reuseIdentifier)

        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)

        self.initialize()
    }

    func initialize() {
        let titleLabel = UILabel(body: .zero)
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(titleLabel)
        self.titleLabel = titleLabel

        NSLayoutConstraint.activate([
            self.contentView.centerXAnchor.constraint(equalTo: self.titleLabel.centerXAnchor),
            self.contentView.centerYAnchor.constraint(equalTo: self.titleLabel.centerYAnchor),
        ])

        self.contentView.backgroundColor = .black
        self.titleLabel.font = UIFont.boldSystemFont(ofSize: 16)
        self.titleLabel.textAlignment = .middle
        self.titleLabel.textColor = .white
    }
}

There may be just a few issues left to do, you need to register your header view, identical to you probably did it for the cells. It is precisely the identical approach, besides that there’s a separate registration “pool” for the header & footer views. Lastly you need to implement two further, however comparatively easy (and acquainted) delegate strategies.



extension ViewController: UITableViewDelegate {

    

    func tableView(_ tableView: UITableView, heightForHeaderInSection part: Int) -> CGFloat {
        return 32
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection part: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! HeaderView
        view.titleLabel.textual content = self.key(for: part)
        return view
    }
}

Footers works precisely the identical as headers, you simply should implement the corresponding information supply & delegate strategies with the intention to assist them.

You may even have a number of cells in the identical desk view primarily based on the row or part index or any particular enterprise requirement. I am not going to demo this right here, as a result of I’ve a approach higher answer for mixing and reusing cells contained in the CoreKit framework. It is already there for desk views as effectively, plus I already coated this concept in my final assortment view tutorial put up. You need to test that too. πŸ€“

Part titles & indexes

Okay, in case your mind just isn’t melted but, I will present you two extra little issues that may be fascinating for learners. The primary one is predicated on two further information supply strategies and it is a very nice addition for lengthy lists. (I desire search bars!) 🀯

extension ViewController: UITableViewDataSource {
    

    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return ["1", "2", "3"]
    }

    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        return index
    }
}

If you will implement these strategies above you’ll be able to have a little bit index view on your sections in the suitable facet of the desk view, so the end-user will be capable to rapidly soar between sections. Identical to within the official contacts app. πŸ“•

Choice vs spotlight

Cells are highlighted when you find yourself holding them down along with your finger. Cell goes to be chosen in the event you launch your finger from the cell.

Do not over-complicate this. You simply should implement two strategies in you customized cell class to make all the things work. I desire to deselect my cells straight away, if they are not for instance utilized by some kind of information picker format. Right here is the code:

class CustomCell: UITableViewCell {

    

    override func setSelected(_ chosen: Bool, animated: Bool) {
        self.coverView.backgroundColor = chosen ? .purple : .clear
    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        self.coverView.backgroundColor = highlighted ? .blue : .clear
    }
}

As you’ll be able to see, it is ridiculously simple, however a lot of the learners do not understand how to do that. Additionally they normally overlook to reset cells earlier than the reusing logic occurs, so the listing retains messing up cell states. Don’t be concerned an excessive amount of about these issues, they’re going to go away as you are going to be extra skilled with the UITableView APIs.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments