微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UICollectionView 在 UItableViewCell 内部没有水平滚动

如何解决UICollectionView 在 UItableViewCell 内部没有水平滚动

我在使用 Xcode 12.2 时遇到了这个问题,因为我无法在水平方向滚动 UICollectionView,我尝试了论坛中的多个答案,但没有一个我有

确切的行为是: 每个 UItableViewCell 中的信息收费罚款,并且部分集合由于不可滚动而隐藏在右侧。

在滚动 UITableViewCells 的同时,你可以向下和向上滚动,并且单元格信息收费,但是每个 UITableViewCells 内的 UICollectionViews 不能水平滚动,并且信息收费很好

首先,我只是想实现水平滚动。

这是我在视图上的 UITableView 声明:

let tableSeasons: UITableView = {
        let table = UITableView(frame: CGRect.zero)
        table.register(NumberOfSeasonsCell.self,forCellReuseIdentifier: "seasonsRegister")
        table.backgroundColor = .clear
        table.layer.cornerRadius = 0
        table.separatorColor = .clear
        table.translatesAutoresizingMaskIntoConstraints = false
        table.showsHorizontalScrollIndicator = false
        table.showsverticalScrollIndicator = false
        return table
    }()

这是 VC 中的 Delegate 和 DataSource 协议:

extension SelectedShowVC: UITableViewDelegate,UITableViewDataSource {
 
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        
        return arrayOfSeasonsInt.count
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        guard let cell = tableView.dequeueReusableCell(withIdentifier: "seasonsRegister",for: indexPath) as? NumberOfSeasonsCell else {
            return UITableViewCell()
        }
        cell.backgroundColor = .clear
        
        cell.selectionStyle = .none
        cell.seasonsCollectionView.delegate = self //here's the delegate and dataSource for the CollectionView
        cell.seasonsCollectionView.dataSource = self

        

        return cell
    }
    
    func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }

    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        
    }
    
    
}

这是在 NumberOfSeasonsCell 或 UITableViewCell 上声明 UICollectionView 的代码

lazy var seasonsCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLinespacing = 15
        let cv = UICollectionView(frame: .zero,collectionViewLayout: layout)
        cv.backgroundColor = UIColor.clear
        cv.register(SeasonCell.self,forCellWithReuseIdentifier: "identifierSeasons")
        cv.showsHorizontalScrollIndicator = false
        cv.translatesAutoresizingMaskIntoConstraints = false
        return cv
    }()

UICollectionView 的协议

extension NumberOfSeasonsCell: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        
            return arraySeasonsEpisodes.count
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifierSeasons",for: indexPath) as! SeasonCell
            cell.backgroundColor = .clear
            cell.model = arraySeasonsEpisodes[indexPath.item] //passing data for the DidSet
            return cell
}
    
    
    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: 150,height: 100)
    }
    
    func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
            return 15
    }
    
    func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
            print(indexPath.row)
    }
 
}

我读过我应该存储表格的最后一个位置来滚动 UICollectionView 但我已经尝试过但对我不起作用

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。