Reputation:
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
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
Reputation: 108
Here are the step to fix your problem
Basically that error you got, is because of mix and match the wrong properties on your table view.
Hope it helps
Upvotes: -1
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
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