Home > Enterprise >  Unable to scroll Collection view inside Table view cell (created in Storyboard)
Unable to scroll Collection view inside Table view cell (created in Storyboard)

Time:06-16

There is a very similar question here, but the solution doesn't solve anything for me, mainly because my embedded collection view is already inside the table view cell's content view (I created it in storyboard).

Is there some setting that I need to check to allow my collection view to scroll? It seems that the parent table view cell is eating up all gestures.

CodePudding user response:

TableViewController.swift

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
    cell.collectionView.dataSource = self
    cell.collectionView.delegate = self
    return cell
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
    return cell
}

}

CustomTableViewCell.swift

class CustomTableViewCell: UITableViewCell {

@IBOutlet weak var collectionView: 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
}

}

You can find my demo project here https://github.com/MattiaPell/CollectionView-inside-a-TableViewCell

CodePudding user response:

Turns out in my case it was simple as not having User Interaction Enabled check marked for my child collection view.

  • Related