首页 > 解决方案 > 无法在表格视图单元格内滚动集合视图

问题描述

我看过类似的问题,但我仍然不确定到底发生了什么。我觉得它可能缺少一些简单的东西,所以如果是这样,我提前道歉。我在表格视图单元格内的集合视图没有滚动,也没有响应 didSelectItemAt方法。该项目编译正确,看起来完全符合测试的需要,但水平滚动和触摸将不起作用。另一个有趣的注意事项是表格视图在自定义单元格中响应didSelectRowAt,但集合视图单元格没有。先感谢您。

根据类似的问题和答案,我尝试了以下方法:

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


标签: iosswift

解决方案


添加到 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
        
}

推荐阅读