Swift:无法滚动浏览在TableViewCell中以编程方式实现的collectionView

问题描述

免责声明:之前也曾提出过类似的问题,但是collectionView的实现并不相同,因此提供的答案无济于事。


我已经在 TvOS 中以编程方式完全实现了这一点:

enter image description here

这是管理collectionView的代码

extension TableViewCell2: UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        if (collectionView == collectionViewTest){
            print("INSIDE TableViewCell2.collectionView 1. collectionViewTest detected")
            print("INSIDE TableViewCell2.collectionView 1. moviesItems.count: ",moviesItems.count)
            // This gets printed 20 times
            
            return moviesItems.count
        }
        else {
            print("first function other collection was detecteddetected")
            print("INSIDE TableViewCell2.collectionView 1. moviesItems2.count: ",moviesItems2.count) //0
            return moviesItems2.count
        }
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print("INSIDE TableViewCell2.collectionView 2")
        // This gets printed 7 times like the number of cells.
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
            cellIdentifier,for: indexPath) as! movieCardCell
        cell.movieImageView.sd_setimage(with: URL(string: moviesItems[indexPath.item].imageURL))
        return cell
    }
}

注意::返回的单元格数为20,但仅显示7。因为仅显示7个collectionView单元格,所以哪一种有意义。

这是在tableViewCell内部实现collectionView的方式:

func setupCollectionView2() {
    let layout = UICollectionViewFlowLayout()
    
    collectionViewTest = UICollectionView(frame: CGRect.zero,collectionViewLayout: layout)
    collectionViewTest.backgroundColor = UIColor (hex: "444A64")
    collectionViewTest.register(UICollectionViewCell.self,forCellWithReuseIdentifier: "MyCell")
    collectionViewTest.showsverticalScrollIndicator = false
    collectionViewTest.showsHorizontalScrollIndicator = false
    self.contentView.addSubview(collectionViewTest)
    collectionViewTest.delegate = self
    collectionViewTest.dataSource = self
    
    
    let nib = UINib(nibName: "movieCardCell",bundle: nil)
    collectionViewTest.register(nib,forCellWithReuseIdentifier: cellIdentifier)
    
    collectionViewTest.translatesAutoresizingMaskIntoConstraints = false
    collectionViewTest.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor,constant: 0).isActive = true
    collectionViewTest.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor,constant: 0).isActive = true
    collectionViewTest.topAnchor.constraint(equalTo: self.contentView.topAnchor,constant: 0).isActive = true
    collectionViewTest.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor,constant: 0).isActive = true
    
    layout.scrollDirection = .horizontal
    
}

问题: 为什么我不能在collectionView中滚动以及如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)