You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift iOS添加第二个独立滚动UICollectionView报错排查

双UICollectionView布局报错问题

实现目标

单页面共展示4行内容,上方2行、下方2行分别归属两个独立UICollectionView,两个集合视图支持独立滚动。
参考资源:

当前故障

项目运行直接触发报错,报错信息参考:

现有代码

正常运行的首个UICollectionView对应ViewController代码

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet var collectionViewButtons: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // 视图加载后额外配置
        collectionViewButtons.delegate = self
        collectionViewButtons.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6 //按钮数量
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ButtonCollectionViewCell
        
        cell.buttonLive.setTitle("Handling a Breakup", for: .normal) //设置按钮标题
        cell.buttonLive.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
        cell.buttonLive.layer.cornerRadius = 10
        cell.buttonLive.clipsToBounds = true
        cell.buttonLive.layer.borderWidth = 1.0
        cell.buttonLive.layer.borderColor = UIColor.white.cgColor

        if indexPath.item == 0 { //第一个按钮
            cell.buttonLive.backgroundColor = UIColor.darkGray //设置按钮背景色
        }
        else if indexPath.item == 1 { //第二个按钮
            cell.buttonLive.backgroundColor = UIColor.systemGray
            cell.buttonLive.setTitle("Good Work", for: .normal)

        }
        else if indexPath.item == 2 { //第三个按钮
            cell.buttonLive.backgroundColor = UIColor.darkGray
        }
        else { //其余按钮
            cell.buttonLive.backgroundColor = UIColor.darkGray
        }
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == 0 { //点击第一个按钮跳转对应页面
            //      let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC1") as! ViewController1
            //        navigationController?.pushViewController(vc, animated: true)
            //      }
            //      else if indexPath.item == 1 {
            //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC2") as! ViewController2
            //      navigationController?.pushViewController(vc, animated: true)
        }
        //      else if indexPath.item == 2 {
        //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC3") as! ViewController3
        //          navigationController?.pushViewController(vc, animated: true)
    }
    //      else {
    //         let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC4") as! ViewController4
    //      navigationController?.pushViewController(vc, animated: true)
    }
    //  }

    //}
    // 修改return后的数值即可调整按钮总数
}

触发报错的第二个UICollectionView对应SecondViewController代码

import UIKit


class SecondViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 视图加载后额外配置

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6 //按钮数量
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let SecondCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SecondCell", for: indexPath) as! ButtonCollectionViewCell
        
        SecondCell.buttonTwo.setTitle("Handling a Breakup", for: .normal) //设置按钮标题
        SecondCell.buttonLive.titleLabel!.font = UIFont(name: "Marker Felt", size: 20)
        SecondCell.buttonTwo.layer.cornerRadius = 10
        SecondCell.buttonTwo.clipsToBounds = true
        SecondCell.buttonTwo.layer.borderWidth = 1.0
        SecondCell.buttonTwo.layer.borderColor = UIColor.white.cgColor

        if indexPath.item == 0 { //第一个按钮
            SecondCell.buttonTwo.backgroundColor = UIColor.darkGray //设置按钮背景色
        }
        else if indexPath.item == 1 { //第二个按钮
            SecondCell.buttonTwo.backgroundColor = UIColor.systemGray
            SecondCell.buttonTwo.setTitle("Good Work", for: .normal)

        }
        else if indexPath.item == 2 { //第三个按钮
            SecondCell.buttonTwo.backgroundColor = UIColor.darkGray
        }
        else { //其余按钮
            SecondCell.buttonTwo.backgroundColor = UIColor.darkGray
        }
        
        return SecondCell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == 0 { //点击第一个按钮跳转对应页面
            //      let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC1") as! ViewController1
            //        navigationController?.pushViewController(vc, animated: true)
            //      }
            //      else if indexPath.item == 1 {
            //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC2") as! ViewController2
            //      navigationController?.pushViewController(vc, animated: true)
        }
        //      else if indexPath.item == 2 {
        //          let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC3") as! ViewController3
        //          navigationController?.pushViewController(vc, animated: true)
    }
    //      else {
    //         let vc = storyboard?.instantiateViewController(withIdentifier: "anyVC4") as! ViewController4
    //      navigationController?.pushViewController(vc, animated: true)
    }
    //  }
    
//}
// 修改return后的数值即可调整按钮总数
}

已尝试排查操作

  • 将代码中所有标识为"collectionView"的字段重命名为第二个UICollectionView的命名"SecondCollection",问题未解决
  • 已为两个UICollectionView分别配置IBOutlet连接,问题未解决
  • 已为两个集合视图单元格内的按钮分别设置独立的IBOutlet连接,问题未解决

内容的提问来源于stack exchange,提问作者Dakota Davis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 22:21:36