You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift开发:UITableViewCell中UIImage赋值报错求助

Fixing the -[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

  1. Open the storyboard or xib file where you created your MealTableViewCell
  2. Select the custom cell, then open the Connections Inspector (the arrow icon in the right-hand panel)
  3. Look for the photoImageView outlet under "Outlets" — you'll see it's connected to ContentView instead of your UIImageView
  4. Click the "x" next to that incorrect connection to delete it
  5. Reconnect the outlet: drag from the circle next to photoImageView in the Connections Inspector directly to the UIImageView in your cell layout
  6. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:28:49