更改收集视图的`numberOfitemSinsection`参数

发布于 2025-01-29 15:09:30 字数 1043 浏览 2 评论 0原文

如何更改集合视图的numberOfitemSinsection参数?

我已经对收集视图进行了基本设置。现在,我尝试制作一个按钮,以更改其中的项目数量。

常规设置是标准设置:

   func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        arrayA.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell            
        cell.imageView.image = arrayA[indexPath.item][0].image
        return cell
    }

问题是 - 如何配置一个按钮,因此它可以从当前arraya.count.count.count更改numberFitemSinsection参数(ex arrayb.count )?

示例:

How is it possible to change numberOfItemsInSection parameter of a collection view?

I have made a basic setup of a collection view. And now I try to make a button, that changes the amount of items in it.

The general setup is a standard one:

   func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        arrayA.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell            
        cell.imageView.image = arrayA[indexPath.item][0].image
        return cell
    }

The question is - how to configure a button, so it could change numberOfItemsInSection parameter from current arrayA.count to some other (e.x. arrayB.count)?

Example: enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2025-02-05 15:09:30

您可以将通用的标志带在arraya和arrayb之间切换。单击按钮时

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return isUseArrayA ? arrayA.count : arrayB.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell           
    cell.imageView.image = isUseArrayA ? arrayA[indexPath.item][0].image : arrayB[indexPath.item][0].image
    return cell
}

@IBAction func changeSource(sender: UIButton) {
    if sender.tag == 0 {
       isUseArrayA = true
       sender.tag = 1
    } else {
       sender.tag = 0
       isUseArrayA = false
    } 
    DispatchQueue.main.async { [weak self] in
        guard let self = self else { return }
        self.collectionView.reloadData()
    }
}

You could take a common a flag to toggle between arrayA and arrayB. When the button is clicked as

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return isUseArrayA ? arrayA.count : arrayB.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell           
    cell.imageView.image = isUseArrayA ? arrayA[indexPath.item][0].image : arrayB[indexPath.item][0].image
    return cell
}

@IBAction func changeSource(sender: UIButton) {
    if sender.tag == 0 {
       isUseArrayA = true
       sender.tag = 1
    } else {
       sender.tag = 0
       isUseArrayA = false
    } 
    DispatchQueue.main.async { [weak self] in
        guard let self = self else { return }
        self.collectionView.reloadData()
    }
}
倒数 2025-02-05 15:09:30
// No need of numberOfSection because it is bydefault 1.

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    arrayA.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    cell.imageView.image = arrayA[indexPath.item][0].image
    return cell
}

@IBAction func changeArrayCount(sender: UIButton) {
    arrayA.append(image)
    DispatchQueue.main.async { [weak self] in
        guard let self = self else { return }
        self.collectionView.reloadData()
    }
}
// No need of numberOfSection because it is bydefault 1.

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    arrayA.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    cell.imageView.image = arrayA[indexPath.item][0].image
    return cell
}

@IBAction func changeArrayCount(sender: UIButton) {
    arrayA.append(image)
    DispatchQueue.main.async { [weak self] in
        guard let self = self else { return }
        self.collectionView.reloadData()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文