Home > other >  Unable to scroll Collection view inside Table view cell
Unable to scroll Collection view inside Table view cell

Time:11-15

I have looked at the similar questions, and I still am not sure exactly what's going on. I feel like it might be something simple that is missing so I apologize in advance if so. My collection view inside table view cell is not scrolling and not responding to didSelectItemAt method. The project compiles correctly and looks exactly as needed for testing, but the horizontal scrolling and touches will not work. Another interesting note is that the table view responds to didSelectRowAt in the custom cell, but the collection view cells do not. Thank you in advance.

Per similar questions and answers, I have tried the following things:

  • cell.selectionStyle = .none for the table cell that contains collection view
  • return nil in the tableViews willSelectRowAt method for the cell with collection view

PostViewController.swift

class PostViewController: UIViewController {
        
    
    private let table: UITableView = {
        let table = UITableView(frame: .zero, style: .grouped)
        table.register(CustomTableCellTableViewCell.self, forCellReuseIdentifier: CustomTableCellTableViewCell.id)
        table.register(UITableViewCell.self, forCellReuseIdentifier: "TableCell")
        return table
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.view.addSubview(self.table)
        self.table.delegate = self
        self.table.dataSource = self
        
        
    }
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.table.frame = self.view.bounds
    }
    

}


extension PostViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        switch indexPath.row {
        case 2:
            guard let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableCellTableViewCell.id,
                                                           for: indexPath) as? CustomTableCellTableViewCell else { return UITableViewCell() }
            return cell
        default:
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath)
            return cell
        }
        
    
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    
}


** CustomTableCellTableViewCell.swift **

class CustomTableCellTableViewCell: UITableViewCell {

    
    static let id = "CustomTableCellTableViewCell"
    
    
    private let collection: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 128, height: 128)
        let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionCell")
        collection.backgroundColor = .systemBlue
        return collection
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        self.addSubview(self.collection)
        self.collection.delegate = self
        self.collection.dataSource = self
        
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
    
    override func layoutSubviews() {
        self.collection.frame = self.bounds
    }
    

}


extension CustomTableCellTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collection.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath)
        cell.backgroundColor = .systemYellow
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Selected")
    }
    
    
}


CodePudding user response:

Subviews added to the UITableViewCell is behind contentView, which is blocking user input from reaching the collection view.

Collection view need to add in content view as -

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(self.collection)
        self.collection.delegate = self
        self.collection.dataSource = self
        
}
  • Related