0
votes

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. 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!




}

}

1

1 Answers

5
votes

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

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!
}