【问题标题】:how to convert a collection view to circular icarousel in swift如何在swift中将集合视图转换为圆形icarousel
【发布时间】:2023-03-08 16:54:02
【问题描述】:

我想像循环列表一样在集合视图中显示单元格,这意味着在集合视图的最后一个单元格之后,滚动集合视图时会显示第一个单元格再次,像 循环链接列表

我尝试过使用 icrousel,但由于 icarosuel 只处理视图,我不想完全完成集合视图并重新开始使用 icarousel,所以有什么办法可以让我的集合视图循环

这是我的collectionView 代码

        func collectionView(_ collectionView: UICollectionView, 
         cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = 
                collectionView.dequeueReusableCell(withReuseIdentifier: 
                "CellName", for: indexPath as IndexPath) as! CellName

        let gigModel = self.datasoruce?[indexPath.row]

        cell.lblTitle.text = gigModel?.title

        cell.btnPrice.setTitle(gigModel?.getPriceAccordingToGigType(), 
         for: .normal)

          cell.itemImageView.sd_setImage(with: URL(string: 
          (gigModel?.getPhotoPath())!), placeholderImage: 
          UIImage.init(named: "place_holder"))

        cell.itemImageView.layer.cornerRadius = 10.0

        if Utilities.isValidString(object: gigModel?.adminId as 
         AnyObject) {
            cell.btnStar.isHidden = false
        }
        else {
            cell.btnStar.isHidden = true
        }

        return cell
         }

我希望这是一个循环列表。

【问题讨论】:

  • 处理collectionView滚动,并根据你的collectionView替换数组元素
  • 我在理解这一点上有点困难,请您多说一点。谢谢

标签: ios swift uicollectionview icarousel


【解决方案1】:

我尝试创建示例项目,它非常简单,这里是示例代码如何实现“无限”滚动

    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var array: [Any] = [0,1,2,3,4,5,6,7,8,9]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Int(Int16.max) // Int.max cause crash
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCell
        let correctIndex = indexPath.row >= array.count ? indexPath.row % array.count : indexPath.row
        cell.nameLabel.text = "\(array[correctIndex])"
        return cell
    }

}

希望对你有帮助

【讨论】:

  • 感谢您的关注,目前正在运行。谢谢。
  • @Alexandr 很棒的解决方案...!它的工作正常......!谢谢...!
【解决方案2】:

//使用下面的代码获取下一个单元格。

func scrollToNextCell(){
    //get Collection View Instance
    let collectionView:UICollectionView;

    //get cell size
    let cellSize = CGSizeMake(self.view.frame.width, self.view.frame.height);

    //get current content Offset of the Collection view
    let contentOffset = collectionView.contentOffset;

    //scroll to next cell
    collectionView.scrollRectToVisible(CGRectMake(contentOffset.x + cellSize.width, contentOffset.y, cellSize.width, cellSize.height), animated: true);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    相关资源
    最近更新 更多