1

I got this example of using custom cells in my Swift project :

let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
                    as! HeadlineTableViewCell

But in my project, I actually have an array of Custom cells called mycells.

So I thought I could simply change it to :

let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
                    as! type(of:allCells[indexPath.row])

But no. The compiler complains about this :

Cannot create a single-element tuple with an element label

Maybe this is just dumb but I cant get why it wont work. Can someone help me and clarify whats going on?

0

3 Answers 3

1

I use something similar in my apps, this is how I resolved this problem

    extension UITableViewCell {
         @objc func configure(_ data: AnyObject) {}
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = info.sectionInfo[indexPath.section].data[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: data.identifier.rawValue, for: indexPath)
        cell.configure(data as AnyObject)
        return cell
    }

    class DefaultCell: UITableViewCell {

    override func configure(_ data: AnyObject) {
        guard let data = data as? MyDesiredClass
            else {
                return
        }
        // do smth
    }

}

in this case you don't need to pass cell type directly, because any cell contains configure func where you can fill all fields

Sign up to request clarification or add additional context in comments.

Comments

0

You issue is some kind of syntax mistake that is not visible in the code you provided.

But Use generic instead:

Define something like this:

protocol Reusable: UIView {
    static var identifier: String { get }
}

extend it for UITableViewCell:

extension Reusable where Self: UITableViewCell {
    static var identifier: String {
        return String(describing: self)
    }
}

Conform cell to it:

extension HeadlineTableViewCell: Reusable {}

add this extension to UITableView:

extension UITableView {
    func dequeueReusableCell<T: UITableViewCell & Reusable>(type cellType: T.Type, for indexPath: IndexPath) -> T {
        return dequeueReusableCell(withIdentifier: cellType.identifier, for: indexPath) as! T
    }
}

and use it like this:

myTableView.dequeueReusableCell(type: HeadlineTableViewCell.self, for: indexPath)

This will dequeue and cast at the same time

Comments

0

I'm assuming that allCells is an array that contains a list of table view cells, but the cells are of different class types.

This line here that you're using can not be used the way that you're trying to use it.

type(of:allCells[indexPath.row])

That's why you're getting the error. This function returns the objects Metatype and you can't use that result in the way you're trying above. You should probably look into how optionals work too and how to unwrap them because the way you're trying to do this isn't going to work. This line you had here below would work fine, but unwrapping using type(of:) syntax is not going to work:

let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! HeadlineTableViewCell

Honestly, the entire architecture of using arrays to store tableViewCells is wrong, and I'm not even sure what you're trying to accomplish by doing that, but I can almost 100% say it's a very bad idea. Instead the array should store the data that the tableViewCell is going to display.

Honestly if I were you I would look up a tutorial on table views because I feel like there's a lot of misunderstanding here of how table views work which has led to the code that you wrote and the problems that you're having.

Check out this tutorial here. It should help you get a better understanding of how things work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.