Skip to content

Latest commit

 

History

History
72 lines (58 loc) · 2.95 KB

UITableViewCell.md

File metadata and controls

72 lines (58 loc) · 2.95 KB

Dequeue Table View Cell Usage

Scans through storyboards.

Xcode Editor Autocomplete

Autocomplete Dequeue Table View Cell

Storyboard Identifier

Account Table View Cell

Backboard Generated Code

AccountStoryboard.blackboard.swift

final func dequeueAccountCell(from tableView: UITableView, for indexPath: IndexPath,
                              initialize: ((_ accountCell: AccountTableViewCell) -> Void)? = nil) -> AccountTableViewCell

final func dequeueAccountOverviewCell(from tableView: UITableView, for indexPath: IndexPath,
                                      initialize: ((_ accountOverviewCell: AccountOverviewTableViewCell) -> Void)? = nil) -> AccountOverviewTableViewCell

final func dequeueOpenNewAccountCell(from tableView: UITableView, for indexPath: IndexPath,
                                     initialize: ((_ cell: UITableViewCell) -> Void)? = nil) -> UITableViewCell

The Blackboard Way

ExampleApp

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch Section(rawValue: indexPath.section)! {
    case .overview:
        return dequeueAccountOverviewCell(from: tableView, for: indexPath) { cell in
            cell.helloLabel.text = self.viewModel.helloText
            cell.rewardsLevelLabel.text = self.viewModel.rewardsLevelText
        }
    case .accounts:
        let accountViewModel = accountViewModels[indexPath.row]
        return dequeueAccountCell(from: tableView, for: indexPath) { cell in
            cell.nameLabel.text = accountViewModel.name
            cell.balanceLabel.text = accountViewModel.balance
        }
    case .openNewAccount:
        return dequeueOpenNewAccountCell(from: tableView, for: indexPath)
    }
}

The Standard UIKit Way

StandardApp

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch Section(rawValue: indexPath.section)! {
    case .overview:
        let cell = tableView.dequeueReusableCell(withIdentifier: "Account Overview", for: indexPath) as! AccountOverviewTableViewCell
        cell.helloLabel.text = viewModel.helloText
        cell.rewardsLevelLabel.text = viewModel.rewardsLevelText
        return cell
    case .accounts:
        let accountViewModel = accountViewModels[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "Account", for: indexPath) as! AccountTableViewCell
        cell.nameLabel.text = accountViewModel.name
        cell.balanceLabel.text = accountViewModel.balance
        return cell
    case .openNewAccount:
        return tableView.dequeueReusableCell(withIdentifier: "Open New Account", for: indexPath)
    }
}