弹出窗口内的collectionview

问题描述

我正在尝试在弹出窗口中使用集合视图。单元格的数据将从应用程序内部的数组和预添加的数据(图像和文本)中获取。我试图创建一个自定义集合视图swift类,并尝试将其添加到弹出窗口swift类中,但是当我尝试将其放入view.addsubview()时却无法正常工作并给我一个致命错误。 关于如何将收藏夹视图插入到弹出窗口中的任何想法?

更新:下面的代码

import UIKit

protocol PopUpDelegate {
    func handledismissal()
}

class PopUpWindow: UIView,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid,for: indexPath) as! TeamCell
        cell.backgroundColor = .cyan
        return cell
    }
    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (self.frame.width / 3) - 16,height: 100)
    }
    func collectionView(_ collectionView: UICollectionView,insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
    }
    
    

    // MARK: - Properties
    
    let cellid = "cellid"
    
    let label: UILabel = {
        let label1 = UILabel()
        label1.translatesAutoresizingMaskIntoConstraints = false
        label1.text = "Tawasol Message"
        label1.textColor = .black
        label1.textAlignment = .center
        label1.backgroundColor = UIColor.red
        return label1
    }()
    
    let collectionview: UICollectionView = {
       let layout = UICollectionViewFlowLayout()
       let cv = UICollectionView(frame: CGRect.zero,collectionViewLayout: layout)
        cv.register(TeamCell.self,forCellWithReuseIdentifier: "cellid")
       //If you set it false,you have to add constraints.
       cv.translatesAutoresizingMaskIntoConstraints = false
//       cv.delegate = self
//       cv.dataSource = self
//       cv.register(HeaderCell.self,forCellWithReuseIdentifier: "HeaderCell")
       cv.backgroundColor = .yellow
       return cv
    }()
    
    let col: MyViewController = {
       let coll = MyViewController()
        
        return coll
    }()
    
    let button: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = UIColor.blue
        button.setTitle("Done",for: .normal)
        button.setTitleColor(.white,for: .normal)
        button.addTarget(self,action: #selector(handledismissal),for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.layer.cornerRadius = 5
        return button
    }()
    
//    var delegate: PopUpDelegate?
    
    // MARK: - Init
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        
        addSubview(label)
        label.heightAnchor.constraint(equalToConstant: 50).isActive = true
        label.leftAnchor.constraint(equalTo: leftAnchor,constant: 12).isActive = true
        label.topAnchor.constraint(equalTo: topAnchor,constant: 12).isActive = true
        label.rightAnchor.constraint(equalTo: rightAnchor,constant: -12).isActive = true
        
        
        addSubview(col as! UICollectionView)
        
        addSubview(button)
        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
        button.leftAnchor.constraint(equalTo: leftAnchor,constant: 12).isActive = true
        button.bottomAnchor.constraint(equalTo: bottomAnchor,constant: -12).isActive = true
        button.rightAnchor.constraint(equalTo: rightAnchor,constant: -12).isActive = true

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Selectors
    
    @objc func handledismissal() {
        print("DONE...")
//        delegate?.handledismissal()
    }
    
}
class TeamCell: UICollectionViewCell{
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    func setup(){
        self.backgroundColor = .red
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

我在另一个swift文件中将collectionView创建为UICollectionView的子类,代码如下:

import UIKit

class MyViewController : UIViewController {
    var myCollectionView:UICollectionView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let view = UIView()
        view.backgroundColor = .white
        
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20,left: 10,bottom: 10,right: 10)
        layout.itemSize = CGSize(width: 60,height: 60)
        
        myCollectionView = UICollectionView(frame: self.view.frame,collectionViewLayout: layout)

        myCollectionView?.register(UICollectionViewCell.self,forCellWithReuseIdentifier: "MyCell")
        myCollectionView?.backgroundColor = UIColor.white
        
        myCollectionView?.dataSource = self
        myCollectionView?.delegate = self
 
        view.addSubview(myCollectionView ?? UICollectionView())
        
        self.view = view
    }
}

extension MyViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return 9 // How many cells to display
    }
    
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell",for: indexPath)
        myCell.backgroundColor = UIColor.blue
        return myCell
    }
}

extension MyViewController: UICollectionViewDelegate {
 
    func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
       print("User tapped on item \(indexPath.row)")
    }
}

当我想显示弹出窗口时出现错误错误显示在弹出的swift文件的初始化中,在addSubview(col as! UICollectionView)中,错误是:

线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

解决方法

发生崩溃是因为您试图将col变量强制转换为UICollectionView,但是在此处将其声明为MyViewController

let col: MyViewController = {
       let coll = MyViewController()
        
        return coll
    }()

假设您没有使用情节提要板,而是用代码完成所有操作,然后为了将某些屏幕显示为弹出窗口,建议执行以下操作:

  1. 创建一个单独的视图控制器,以显示您的弹出内容。
  2. 将要显示在弹出窗口中的视图添加为该视图控制器视图的子视图。
  3. 在演示视图控制器中,创建此新视图控制器的实例,并通过设置模式视图控制器的modalPresentationStyle属性来设置其所需的模式表示样式。
  4. 调用呈现视图控制器的present(_:animated:completion:)方法,将模式视图控制器的实例作为参数传递。