Daniel
Daniel

Reputation: 55

How to access the methods for my custom UITableViewCell in my viewController

I have a question about accessing methods that I’ve added to a custom class. I created a custom UITableViewCell class and added two methods to it. I’m trying to access those methods in my view controller but somehow don’t see them as options. I’ve registered the cell in a function that runs in viewDidLoad() and I create an instance (I think) within cellForRowAt indexPath when run cell = UITableViewCell.init(style: .value1, reuseIdentifier: cellId) as! CustomCell1. Right after I dequeue the cell I try doing cell.configure(... but don’t see my “configure” method as an option. Am I doing something wrong?

This is the code for my custom cell:

import UIKit

class CustomCell1: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure() {
        print("Cell got configured")
    }
}

And this is my View Controller:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let cellId = "cell"

    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
    }

    func setupTableView() {
        let table = UITableView()
        table.delegate = self
        table.dataSource = self
        view.addSubview(table)
        table.register(CustomCell1.self, forCellReuseIdentifier: cellId)
        table.translatesAutoresizingMaskIntoConstraints = false
        table.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        table.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        table.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        table.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch section {
        case 0:
            return "Value 1 Cell Style | Section: \(section) - 🍔"
        default:
            return "Default cell header title"
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        switch indexPath.section {
        case 0:
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: cellId) as! CustomCell1
            cell.configure(...) // This method isn't working
            cell.textLabel?.text = "Title Label \(indexPath.row) - 🍉"
            cell.detailTextLabel?.text = "Detail label"
        default:
            cell = UITableViewCell.init(style: .default, reuseIdentifier: cellId)
            cell.textLabel?.text = "Title Label \(indexPath.row) - 🍳"
            cell.detailTextLabel?.text = "Detail Label"
        }

        return cell
    }
}


Upvotes: 0

Views: 506

Answers (1)

Ahmed Elgendy
Ahmed Elgendy

Reputation: 1700

Your using of dequeueReusableCell is not the right way,

To make your code better: use different cell identifier for the custom and the default cell

let cell1Id = "cell1"        
let defaultCellId = "defaultCell"

then register the cells

table.register(CustomCell1.self, forCellReuseIdentifier: cell1Id)
table.register(UITableViewCell.self, forCellReuseIdentifier: defaultCellId)

Now you can let dequeueReusableCell function to create and reuse the cells:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.section {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: cell1Id, for: indexPath) as! CustomCell1
        cell.configure()
        cell.textLabel?.text = "Title Label \(indexPath.row) - 🍉"
        cell.detailTextLabel?.text = "Detail label"
        return cell
    default:
        let cell = tableView.dequeueReusableCell(withIdentifier: defaultCellId, for: indexPath)
        cell.textLabel?.text = "Title Label \(indexPath.row) - 🍳"
        cell.detailTextLabel?.text = "Detail Label"
        return cell
    }
}

Happy coding!

Upvotes: 1

Related Questions