Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当使用collectionView作为容器时,如何插入新屏幕?_Ios_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 当使用collectionView作为容器时,如何插入新屏幕?

Ios 当使用collectionView作为容器时,如何插入新屏幕?,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我正在尝试创建一个应用程序,它使用collectionView作为屏幕间分页的基础。对于其中一个功能,我想在用户点击按钮时插入屏幕(collectionView单元格),并在用户再次点击按钮时移除屏幕(collectionView单元格)。我曾尝试实现下面所示的插入和删除方法,但是当调用它们时,似乎只是将单元格向下滑动,而不允许我修改它们。当单元格向下滑动时,它会创建一个有问题的用户体验。我如何添加一个单元格并修改它,然后删除它而不影响用户体验 下面是添加和删除单元格的代码: func addC

我正在尝试创建一个应用程序,它使用collectionView作为屏幕间分页的基础。对于其中一个功能,我想在用户点击按钮时插入屏幕(collectionView单元格),并在用户再次点击按钮时移除屏幕(collectionView单元格)。我曾尝试实现下面所示的插入和删除方法,但是当调用它们时,似乎只是将单元格向下滑动,而不允许我修改它们。当单元格向下滑动时,它会创建一个有问题的用户体验。我如何添加一个单元格并修改它,然后删除它而不影响用户体验

下面是添加和删除单元格的代码:

func addCell(sender:Any?){
    self.numberOfItemsInSections += 1
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: IndexPath(item:1,section:0)) as! customCell
    cell.backgroundColor = .purple
    //newCell.label.text = "new cell added"
    self.collectionView.insertItems(at: [IndexPath(item:1,section:0)])

}

func removeCell(sender: Any?) {
    self.numberOfItemsInSections -= 1
    self.collectionView.deleteItems(at: [IndexPath(item:1,section:0)])


}
下面是完整的代码

import UIKit

class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,addRemoveCell {


    @IBOutlet weak var collectionView: UICollectionView!

    var numberOfItemsInSections:Int = 3

    var cellType:[String] = ["customCell"]
    var cellClass:[Any]!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(customCell.self, forCellWithReuseIdentifier: "customCell")
        collectionView.isScrollEnabled = true
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        collectionView.collectionViewLayout = layout
        collectionView.isPagingEnabled = true
    }

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

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

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as!  customCell
        cell.addRemoveCellDelegate = self
        cell.label.text = "\(indexPath)"
        cell.backgroundColor = .blue
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func addCell(sender:Any?){
        self.numberOfItemsInSections += 1
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: IndexPath(item:1,section:0)) as! customCell
        cell.backgroundColor = .purple
        //newCell.label.text = "new cell added"
        self.collectionView.insertItems(at: [IndexPath(item:1,section:0)])

    }

    func removeCell(sender: Any?) {
        self.numberOfItemsInSections -= 1
        self.collectionView.deleteItems(at: [IndexPath(item:1,section:0)])


    }



}

protocol addRemoveCell{
    func addCell(sender:Any?)
    func removeCell(sender:Any?)
}

class customCell:UICollectionViewCell{

    var button:UIButton!
    var label:UILabel!
    var addRemoveCellDelegate:addRemoveCell?
    var addCell:Bool = true

    override init(frame: CGRect) {
        super.init(frame: frame)
         self.button = UIButton(frame: CGRect(x: self.frame.midX, y: self.frame.midX, width: self.frame.midX, height: self.frame.midX))
        self.addSubview(button)


        label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height/8))
        self.addSubview(label)
        label.textAlignment = .center
        label.adjustsFontSizeToFitWidth = true

        label.backgroundColor = .green
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        button.backgroundColor = .orange

        button.translatesAutoresizingMaskIntoConstraints = false
        button.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0).isActive = true
        button.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0).isActive = true
        button.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -18).isActive = true
        button.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 18).isActive = true
        button.heightAnchor.constraint(equalToConstant: 70).isActive = true
    }

    @objc func  buttonTapped(){
    print("The button was tapped")
        if addCell{
        if let delegate = addRemoveCellDelegate{
            print("Inside of addCell")
            delegate.addCell(sender: self)
        }
            addCell = !addCell
        }
        else{
            if let delegate = addRemoveCellDelegate{
              delegate.removeCell(sender: self)
            }
            addCell = !addCell
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


} 

您所说的“修改它们”“到底是什么意思?@0xa6a我想更改用作屏幕的自定义UICollectionViewCell的属性。我想将插入的单元格强制转换为某个自定义UICollectionViewCell,然后调整其属性,就像在
cellForItemAt
函数中一样。是否尝试在
cellForItemAt
方法中进行修改?@0xa6a我尝试过这一点,但它会基于indexPath static分配单元格,因此当我尝试添加新屏幕时在中,cellForItemAt以应用程序开始时的相同顺序加载所有单元格,在collectionview的末尾添加另一个单元格,而不修改刚刚插入的单元格。