I am Trying to Create an Expandable Table View With Child Element Having a UICollection View

70 Views Asked by At

I am Trying to Create a Table View which is

  1. Expandable
  2. It's Childview has a UICollectionView
  3. UICollectionView is Dynamically rendered using API
  4. onTouch Event of UICollectionView Item should be taken care of

Tried couple of samples didn't work

import UIKit

extension String{
    func print(){
        Swift.print(self)
    }

    func log(){
        Swift.print(self)
    }
}


class CustomVCViewController: UIViewController, UITableViewDataSource, UITableViewDelegate , UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ccell", for: indexPath) as! CustomCollectionViewCell
        cell.backgroundColor = UIColor.blue
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return 10
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        "Selection of Collection View item at \(indexPath.row)".print()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        "Selection of Table View item at \(indexPath.row)".print()
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! CustomTableViewCell
        cell.innerCollectionView.dataSource = self
        cell.innerCollectionView.delegate = self
        cell.backgroundColor = UIColor.red

        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()


        tableView.delegate = self

        tableView.dataSource = self
        // Do any additional setup after loading the view.
    }
}
  1. This is for the table view cell
 import UIKit        
 class CustomTableViewCell: UITableViewCell {

        @IBOutlet weak var innerCollectionView: UICollectionView!
        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
        }
 }
1

There are 1 best solutions below

0
Shivam Gaur On

According to my understanding you want the tableviewcell to be expandable as collection view so Im adding the code for it , the thing is collectionview is not invalidating its first internsic content size accordingly

    /// This CollectionView class is used to invalidate collectionview's default implementation of inrinsic content size

    class DynamicCollectionView: UICollectionView {
        override func layoutSubviews() {
            super.layoutSubviews()
            if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
                self.invalidateIntrinsicContentSize()
            }
        }

        override var intrinsicContentSize: CGSize {
            return collectionViewLayout.collectionViewContentSize
        } 
}

SO add this class to your collectionview and provide flowlayout to your collectionviewcells. also!!! return UITableViewAutomaticDimension on heightforRowAt of tableview.