swiftxcodeuitableviewaccessoryview

How to add different accessories(or subviews) for each cell in swift?


I made 4 identical cells with subviews by using UITableViewCell subclass 'FruitTableViewCell' class.

FruitTableViewCell.swift

class FruitTableViewCell: UITableViewCell, UITextFieldDelegate {
    var fruitsTextField = UITextField()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(fruitsTextField)
    }

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

    override func layoutSubviews() {
        super.layoutSubviews()
        fruitsTextField.frame = CGRect(x: 100, y: 7.5, width: 50, height: 30)
        fruitsTextField.backgroundColor = UIColor.darkGray
        fruitsTextField.delegate = self

    }
}

TableViewController.swift

class TableViewController: UITableViewController, UITextFieldDelegate {
    let fruitsComponents: [String] = ["Apple", "Banana", "Grape", "Pear"]
    let cellReuseidentifier = "cell"

    override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(FruitTableViewCell.self, forCellReuseIdentifier: cellReuseidentifier)

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
    sections

    return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return fruitsComponents.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseidentifier, for: indexPath) as! FruitTableViewCell
        cell.textLabel?.text = fruitsComponents[indexPath.row]
        return cell

    }
}

It works well.

But in fact, I want to add different accessories(or subviews) for each cell. Row 0 for UITextField, Row 1 for UILabel, Row 2 for Stepper, Row 3 for UILabel, ... and so on.

So I made the other UITableViewCell subclass 'AnotherFruitTableViewCell' class to use.

And I tried by using 'if' statement.

revised TableViewController.swift

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseidentifier, for: indexPath) as! FruitTableViewCell
        cell.textLabel?.text = fruitsComponents[indexPath.row]
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseidentifier, for: indexPath) as! AnotherFruitTableViewCell
        cell.textLabel?.text = fruitsComponents[indexPath.row]
        return cell
    }

But the message 'could not cast value of type' poped up. Because of this code, I think.

override func viewDidLoad() {

    super.viewDidLoad()

    tableView.register(FruitTableViewCell.self, forCellReuseIdentifier: cellReuseidentifier)

}

And fundamentally, I think 'if' statement is not a good way to add different accessories for each cells.

How can I add different accessories(or subviews) for each cell?


Solution

  • You registered your FruitTableViewCell but not registered AnotherFruitTableViewCell