Swift:如何正确地重用ViewController

时间:2018-08-16 07:09:43

标签: ios swift cell viewcontroller collectionview

我有一个UICollectionViewController类型的HomeController,它处理一些PictureCells(包含图片和标签)。 现在,我将一个PictureCell发送到另一个ViewController以编辑标签。所有这一切都完美。我可以使用协议将其发送回HVC,但我不想走回头一步,而是在新的ViewController中显示编辑后的PictureCell。

我将创建现有HomeController的子类以重用其CollectionView,而不是创建一个全新的HomeController。一切正常,并且显示了视图,但是编辑的PictureCell根本没有显示,即使我可以显示它的图层,Cell本身及其内容也不会显示。

(Messy)类图看起来像这样: ClassDiagram

    class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout, MyProtocol {


    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = UIColor.black
        collectionView?.register(PictureCell.self, forCellWithReuseIdentifier: Constants.cellId)
    }


    //MARK: Get value from second VC
    var valueSentFromSecondViewController: String?
    var cellSentFromSecondViewController: PictureCell?

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        //Acting with Protocol here
    }
     //HERE ARE THE COLLECTIONVIEWFUNCTIONS
}


class PictureEditViewController: UIViewController, UITextFieldDelegate {

    var delegate:MyProtocol?
    var myCell: PictureCell?

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        return cv
    }()

    init(pictureCellInit: PictureCell?) {
        self.myCell = pictureCellInit
        super.init(nibName: nil, bundle: nil)
    }

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

    @objc func showNextVC() {
        let newViewController = PictureShowViewController(withCell: self.myCell)
        newViewController.modalPresentationStyle = .overCurrentContext
        present(newViewController, animated: true) //with dismiss
    }


    @objc func showThings() {
        view.addSubview(collectionView)
        self.collectionView.frame = CGRect(x: x, y: y, width: width, height: height)
        self.setupViews()
    }

    func setupViews() {
        //ADDING THE SUBVIEWS
    }

    func confBounds() {
        //LAYOUT
    }

    @objc func handleDismiss() {
        self.dismiss(animated: true, completion: nil)
    }


class PictureShowViewController: HomeController {

    //MARK: Variable/Constant declaration
    var myCellToShow: PictureCell?

    init(withCell: PictureCell?) {
        let layoutUsing = UICollectionViewFlowLayout()
        myCellToShow = withCell
        super.init(collectionViewLayout: layoutUsing)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.backgroundColor = .white
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        collectionView?.addSubview(myCellToShow!)
        self.collectionView?.reloadData()
    }

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


    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = myCellToShow {
            cell.layer.borderColor = UIColor.red.cgColor
            cell.layer.borderWidth = 2
            return cell
        }
        else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Constants.cellId, for: indexPath) as! PictureCell
            return cell
        }
    }

    //Size of Cell
    override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let cellSize = CGFloat(view.frame.width)
        return CGSize(width: cellSize, height: cellSize)
    }
}

有人知道我哪里出问题了吗?

1 个答案:

答案 0 :(得分:1)

使用PictureCell在视图控制器中移动数据是一个坏主意。 UICollectionView重用单元格的实例,因此单元格的内容随时都可以更改。

因此,不要使用您的单元格,而是移交基础数据并将数据插入到新出列的集合视图单元格中。

最后, var cellSentFromSecondViewController: PictureCell? 应该是var pictureFromSecondViewController: YourPictureData

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Constants.cellId, for: indexPath) as! PictureCell

    //Set your data
    cell.picture = pictureFromSecondViewController

    return cell
}