0

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
        }
 }
Vikash Kumar
  • 642
  • 1
  • 11
  • 25
Rahul Pahuja
  • 31
  • 10
  • 1
    Add the code that you've already tried. Also what do you mean by **Expandable TableView**? – PGDev May 13 '19 at 10:37
  • You won't really get a good answer with a question like this. Please provide details and your research. If you can post your code that you have tried and did not work would be a great help for others. – kerry May 13 '19 at 10:42
  • This blog post might solve what you're looking for - https://medium.freecodecamp.org/how-to-make-height-collection-views-dynamic-in-your-ios-apps-7d6ca94d2212 – PGDev May 13 '19 at 10:47
  • Unfortunately It doesn't work – Rahul Pahuja May 13 '19 at 10:57
  • and the code above doesnt have any expanble-ness as of now – Rahul Pahuja May 13 '19 at 10:58
  • You want the table view cell to be expandable according to the collectionview layout right ? – Shivam Gaur May 13 '19 at 11:57

1 Answers1

0

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.

Shivam Gaur
  • 491
  • 5
  • 16