Swift开发:UITableViewCell中UIImage赋值报错求助
-[UITableViewCellContentView setImage:] Unrecognized Selector Error Hey there, let's break down what's happening here and fix that crash!
What's Causing the Crash?
The error message -[UITableViewCellContentView setImage:]: unrecognized selector sent to instance tells us exactly what's wrong: your code is trying to call setImage(_:) on a UITableViewCellContentView object, but that class doesn't have this method.
This almost always means your photoImageView outlet in MealTableViewCell is incorrectly connected in your storyboard/xib file. Instead of linking to the UIImageView you added to the cell, it's accidentally linked to the cell's built-in contentView.
Step-by-Step Fix
- Open the storyboard or xib file where you created your
MealTableViewCell - Select the custom cell, then open the Connections Inspector (the arrow icon in the right-hand panel)
- Look for the
photoImageViewoutlet under "Outlets" — you'll see it's connected toContentViewinstead of yourUIImageView - Click the "x" next to that incorrect connection to delete it
- Reconnect the outlet: drag from the circle next to
photoImageViewin the Connections Inspector directly to theUIImageViewin your cell layout - Clean your project (press
Cmd + Shift + K) and rebuild/run
Quick Checks to Verify
- Double-check that the image view in your cell has its class set to
UIImageView(not some other class) in the Identity Inspector - Confirm your cell reuse identifier (
MealTableViewCell) matches exactly between your code and storyboard/xib
Your Code for Reference
Here's your sample meal loading function, formatted properly:
private func loadSampleMeals(){ let photo1 = UIImage(named: "meal1") let photo2 = UIImage(named: "meal2") let photo3 = UIImage(named: "meal3") guard let meal1 = Meal(name: "meal1", photo: photo1, rating: 4) else { fatalError("Unable to initialize meal1") } guard let meal2 = Meal(name: "meal2", photo: photo2, rating: 3) else{ fatalError("Unable to initialize meal2") } guard let meal3 = Meal(name: "meal3", photo: photo3, rating: 1) else{ fatalError("Unable to initialize meal3") } meals += [meal1, meal2, meal3] }
Your cellForRowAt method:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentifier = "MealTableViewCell" guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MealTableViewCell else { fatalError("The dequeued cell is not an instance of MealTableViewCell.") } let meal = meals[indexPath.row] cell.nameLabel.text = meal.name cell.photoImageView.image = meal.photo // <- error here cell.ratingControl.rating = meal.rating return cell }
Your custom cell class:
class MealTableViewCell: UITableViewCell { //MARK: Properties @IBOutlet weak var nameLabel: UILabel! @IBOutlet weak var photoImageView: UIImageView! @IBOutlet weak var ratingControl: RatingControl! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
内容的提问来源于stack exchange,提问作者Souvik Ghosh

