簡體   English   中英

無法在表視圖單元格內滾動集合視圖

[英]Unable to scroll Collection view inside Table view cell

我看過類似的問題,但我仍然不確定到底發生了什么。 我覺得這可能是缺少一些簡單的東西,所以如果是這樣,我提前道歉。 我的表視圖單元格內的集合視圖沒有滾動,也沒有響應 didSelectItemAt方法。 該項目編譯正確,看起來完全符合測試需要,但水平滾動和觸摸不起作用。 另一個有趣的注意事項是表格視圖響應自定義單元格中的didSelectRowAt ,但集合視圖單元格沒有。 先感謝您。

根據類似的問題和答案,我嘗試了以下操作:

  • cell.selectionStyle = .none用於包含集合視圖的表格單元格
  • 在帶有集合視圖的單元格的 tableViews willSelectRowAt方法中return nil

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")
    }
    
    
}


添加到 UITableViewCell 的子視圖位於 contentView 后面,這會阻止用戶輸入到達集合視圖。

集合視圖需要在內容視圖中添加為 -

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
        
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM