user7127154
user7127154

Reputation:

Could not cast value of type 'UITableViewCell' to 'ProjectName.UserCell'

Hey guys befor you mark this as a duplicate just please hear me out. I have tried everything related to this topic including adding

self.tableView.registerClass(UserCell.self, forCellReuseIdentifier: "cell")

I have also changed my placeholder cell to match the UserCell class

view with placeholder cell changed to UserCell

Im Not sure what it could be! I get the error :

Could not cast value of type 'UITableViewCell' (0x1134700e0) to 'Lightning_Chat.UserCell'

with type SIGBRT not sure what could be going on and ive tried everything please help!

here is table view code :

  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    // not casting for some reason 
    let cell : UserCell =  UITableViewCell(style: .subtitle , reuseIdentifier: "cellId") as! UserCell

    cell.textLabel?.text = contacts[indexPath.row].userName
    cell.detailTextLabel?.text = contacts[indexPath.row].score


    if let profileImageUrl = contacts[indexPath.row].picURL {
        let url = URL(string: profileImageUrl)
        URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in


            //download hit an error
            if error != nil {
                print(error!)
                return
            }
            DispatchQueue.main.async {
                cell.profileImageView.image = UIImage(data: data!)

            }

        }).resume()
    }


    return cell;
}

here is my view did load :

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.register(UserCell.self, forCellReuseIdentifier: "cellId")

    //irrelevant 
    fetchUsers()
}

Upvotes: 1

Views: 662

Answers (3)

Parama Dharmika
Parama Dharmika

Reputation: 108

Here are the step to fix your problem

  1. Change your table view content to be dynamic cell, instead of static cell
  2. Maintain the code on your viewDidLoad function self.tableView.register(UserCell.self, forCellReuseIdentifier: "cellId"). Also don't forget to connect table view delegate and datasource
  3. On your table view cellForRowAt index path method, don't instantiate the cell directly. Instead use let cell = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!;

Basically that error you got, is because of mix and match the wrong properties on your table view.

Hope it helps

Upvotes: -1

rmaddy
rmaddy

Reputation: 318804

You are not creating the cell correctly.

Change:

let cell : UserCell =  UITableViewCell(style: .subtitle , reuseIdentifier: "cellId") as! UserCell

to:

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

Note that your code is directly creating a UITableViewCell which is why you can't cast it. You are bypassing the standard cell reuse as well.

Upvotes: 1

TNguyen
TNguyen

Reputation: 1051

Instead of:

let cell : UserCell =  UITableViewCell(style: .subtitle , reuseIdentifier: "cellId") as! UserCell

Try this:

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

Upvotes: 2

Related Questions