UICollectionView在水平滚动时未加载新单元格

时间:2018-09-27 17:57:14

标签: swift uicollectionview uicollectionviewcell

我将水平滚动 UICollectionViewController放置在另一个UICollectionViewController单元格内。我这样做是因为我想创建类似效果的图像滑块。

水平滚动UICollectionViewController的代码:

class BusinessPhotosViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout{

    var photos = [String]()
    let cellId = "photos"

    override func viewDidLoad() {
        super.viewDidLoad()

        if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
            layout.minimumLineSpacing = 0
            layout.minimumInteritemSpacing = 0
        }

        collectionView?.backgroundColor = .red
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.alwaysBounceVertical = false
        collectionView?.isPagingEnabled = true
        collectionView?.isScrollEnabled = true

        collectionView?.register(BusinessPhotoCells.self, forCellWithReuseIdentifier: cellId)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! BusinessPhotoCells

        if photos.count != 0 {
            let imageUrl = photos[indexPath.item]
            let url = URL(string: imageUrl)
            cell.logoImage.kf.setImage(with: url)
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.height-10, height: view.frame.height-10)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
}

在另一个类中,这样添加水平滚动UICollectionViewController:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //ADDING SCROLL CELL HERE
        if indexPath.section == 1 {
            let scrollCell = collectionView.dequeueReusableCell(withReuseIdentifier: pictureCellId, for: indexPath)

            scrollCell.backgroundColor = .blue

            let bizVC = BusinessPhotosViewController(collectionViewLayout: UICollectionViewFlowLayout())

            scrollCell.addSubview(bizVC.view)

            bizVC.view.anchor(top: scrollCell.topAnchor, left: scrollCell.leftAnchor, bottom: scrollCell.bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: scrollCell.frame.width, height: scrollCell.frame.height)

            return scrollCell
        }

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: listCellId, for: indexPath) as! CouponCell


        return cell
    }

编辑:每个评论添加numberOfItemsInSection

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 0
        }
        if section == 1 {
            return 1
        }

        return coupons.count
    }

我在本节中返回1,因为我只想要1个水平滚动单元...在水平滚动单元下面(在第2节中),我有一个按预期工作的垂直滚动单元列表。

结果是这样的,其中仅加载了3个正方形,而不是预期的10个正方形:

enter image description here

编辑:添加我现在喜欢的图像 enter image description here

0 个答案:

没有答案