[英]outlets cannot be connected to repeating content UIImageView error?
I get an error stating "The imageView outlet from the ViewController to the UIImageView is invalid. Outlets cannot be connected to repeating content" wI added an imageView in a tableViewCell on my storyboard. 我收到一条错误消息,指出“从ViewController到UIImageView的imageView插座无效。插座无法连接到重复内容”我在故事板上的tableViewCell中添加了imageView。 What should I do to fix this problem? 我应该怎么做才能解决这个问题?
@IBOutlet weak var imageView: UIImageView!
var posts = [postStruct]()
override func viewDidLoad() {
super.viewDidLoad()
let ref = Database.database().reference().child("Posts")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.childrenCount)
for rest in snapshot.children.allObjects as! [DataSnapshot] {
guard let value = rest.value as? Dictionary<String,Any> else { continue }
guard let title = value["Title"] as? String else { continue }
guard let downloadURL = value["Download URL"] as? String else { continue }
self.imageView.sd_setImage(with: URL(string: downloadURL), placeholderImage: UIImage(named: "placeholder.png"))
let post = postStruct(title: title)
self.posts.append(post)
}
self.posts = self.posts.reversed(); self.tableView.reloadData()
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title
return cell!
}
} }
You cannot draw an outlet to the repeating elements in the cell. 您不能为单元格中的重复元素画一个出口。 Hence, there are two options:- Either make a class of the cell, and then you can make outlets in that very class.Using Class is the best option as it will be helpful when there will be a case of no image found for any reason. 因此,有两个选择:-要么创建一个单元格的类 ,然后您就可以在该类中创建插座。使用类是最好的选择,因为在任何情况下都找不到图像的情况下,它将很有帮助原因。
So, always prefer making class of the cell and then setting values to the elements. 因此,始终喜欢对单元格进行分类,然后为元素设置值。
Or, other options is using tags which is a quick fix:- click on UIImageView
, then from the identity inspector go to tags and set some unique integer value to it say 350. Then in your cellForRowAt
或者,其他选项使用的标签是一种快速修复的方法:-单击UIImageView
,然后从身份检查器转到标签,并为其设置一些唯一的整数值,例如350。然后在cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let yourImageView = cell?.viewWithTag(350) as! UIImageView
//Set image to ur yourImageView
return cell!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.