无法在 TableCellView 中初始化 UIImage
Unable to initialise UIImage in TableCellView
我正在学习Swift所以这个问题可能有点傻,请多多包涵。
我有一个函数可以像这样初始化属性-
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]
}
然后我尝试将它们加载到 table 单元格中-
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
}
在上面的代码中分配图像 属性 时出现以下错误-
[UITableViewCellContentView setImage:]: unrecognized selector sent to
instance 0x7fa19ed019a0 2018-05-18 14:12:28.845232+0530
FoodTracker[2440:67077] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[UITableViewCellContentView
setImage:]: unrecognized selector sent to instance 0x7fa19ed019a0'
我已经添加了图片,它们是 .jpg 格式,名称为 meal1
、meal2
和 meal3
。但是不知道我做错了什么。有趣的是,我有另一个图像 - defaultPhoto
在辅助中,如果我注释掉上面抛出错误的行,我的代码运行正常并且模拟器显示 defaultPhoto
image.
这是我的资产的样子-
请问有什么帮助吗?让我知道是否可以添加更多详细信息。
编辑:
我的网点是这样的-
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
}
}
问题出在 photoImageView
上。其他两个网点工作正常。
请检查您的 IBOutlet for UIImageView 是否与您的 Cell 绑定。
您的问题是您错误地将 UITableViewCell
的 contentView 连接为名为 photoImageView
的 UIImageView
,它肯定没有导致崩溃的成员图像
您好,我已经通过图片解释了我的答案,希望这对您有所帮助
[UITableViewCellContentView setImage:]: unrecognized selector sent to instance
表示您的单元格中的 photoImageView outlet 并不是真正的 UIImageView。出于某种原因,photoImageView 插座似乎改为指向单元格的内容视图。我的猜测是您错误地将单元格的 contentview 连接到了这个插座而不是 imageview。
此外,当发生此类错误时,有时在 XCode 中执行 "Clean" 会有所帮助。
我正在学习Swift所以这个问题可能有点傻,请多多包涵。
我有一个函数可以像这样初始化属性-
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]
}
然后我尝试将它们加载到 table 单元格中-
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
}
在上面的代码中分配图像 属性 时出现以下错误-
[UITableViewCellContentView setImage:]: unrecognized selector sent to instance 0x7fa19ed019a0 2018-05-18 14:12:28.845232+0530 FoodTracker[2440:67077] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView setImage:]: unrecognized selector sent to instance 0x7fa19ed019a0'
我已经添加了图片,它们是 .jpg 格式,名称为 meal1
、meal2
和 meal3
。但是不知道我做错了什么。有趣的是,我有另一个图像 - defaultPhoto
在辅助中,如果我注释掉上面抛出错误的行,我的代码运行正常并且模拟器显示 defaultPhoto
image.
这是我的资产的样子-
请问有什么帮助吗?让我知道是否可以添加更多详细信息。
编辑:
我的网点是这样的-
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
}
}
问题出在 photoImageView
上。其他两个网点工作正常。
请检查您的 IBOutlet for UIImageView 是否与您的 Cell 绑定。
您的问题是您错误地将 UITableViewCell
的 contentView 连接为名为 photoImageView
的 UIImageView
,它肯定没有导致崩溃的成员图像
您好,我已经通过图片解释了我的答案,希望这对您有所帮助
[UITableViewCellContentView setImage:]: unrecognized selector sent to instance
表示您的单元格中的 photoImageView outlet 并不是真正的 UIImageView。出于某种原因,photoImageView 插座似乎改为指向单元格的内容视图。我的猜测是您错误地将单元格的 contentview 连接到了这个插座而不是 imageview。
此外,当发生此类错误时,有时在 XCode 中执行 "Clean" 会有所帮助。