NickCoder
NickCoder

Reputation: 1530

set image to imageview in uicollectionview that is in uitableviewcell xib

I have taken a UITableView and deque a UITableViewCell XIB and set data to a label in it, also I have UICollectionView in UITableViewCell XIB in which I have to set images in a UICollectionViewCell.

UIImageView is in UICollectionViewCell and respective UICollectionView is in UITableViewCell so how to set an image to that UIImageView?

also have tableview delegate function in one file

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.discoverData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryTableViewCell", for: indexPath) as! discoveryTableViewCell
    let data = discoverData[indexPath.row]
    cell.messageLbl.text = (data["content"] as! String)

    // This how normally image is set in tableview, how to do with collectionview
    cell.userImage.pin_updateWithProgress = true
    let riderImage = URL(string: (data["profile_pic"] as! String))
    cell.userImage.pin_setImage(from: riderImage, placeholderImage: nil, completion: nil)
    return cell
}

collection view delegate function in UITableViewCell.

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return numCell
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "discoverCell", for: indexPath as IndexPath) as! discoverCollectionViewCell
    cell.postImage.backgroundColor = .cyan
    return cell
}

Here is a picture of how it looks yellow box is UICollectionView and cyan color box is UIImageView.

enter image description here

Upvotes: 0

Views: 452

Answers (1)

Amit
Amit

Reputation: 4886

In cellForRowAt indexPath:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryTableViewCell", for: indexPath) as! discoveryTableViewCell
    if let data = discoverData[indexPath.row] as? [String: Any] {
        cell.messageLbl.text = (data["content"] as! String)

        cell.data = data
    }
    return cell
}

In your "discoveryTableViewCell" (It should be "DiscoveryTableViewCell") :

var data: [String: Any] {
    didSet {
        collectionView.reloadData()
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "discoverCell", for: indexPath as IndexPath) as! discoverCollectionViewCell

    if let profilePic = data["profile_pic"] as? String {
        let riderImage = URL(string: profilePic)
        cell.postImage.pin_setImage(from: riderImage, placeholderImage: nil, completion: nil)
    }

    return cell
}

You should follow the proper style guide and naming conventions in Swift. Please check this URL for better understanding:

Upvotes: 1

Related Questions