FirstCollectionViewController

FirstCollectionViewController

我已经创建了一个包含4个单元格的集合 View ,当您按单元格1中的按钮时(我尚未创建其他单元格),它将带您到一个名为“FirstCollectionViewController”的新ViewController中。我有一个pangesturderecognizer,当您在集合 View 中滑动时会显示滑出菜单。

但是,当您位于“FirstCollectionViewController”中并想返回到集合 View 时,可以按左上角的一个按钮。但是当我按下它时,我将在CollectionViewController的ViewDidLoad中的“self.view.addGestureRecognizer(self.revealViewController()。panGestureRecognizer())”处收到EXC_BAD_INSTRUCTION错误。我试图将panGestureRecognizer放在ViewDidAppear中,但同样发生

我该如何解决?

顺便说一句,应该将您带回到集合 View 的segue称为:“SegueFirstCollectionViewController”

CollectionViewController:

import Foundation
import UIKit

class CollectionViewController: UICollectionViewController {


    var Array = [String]()
    var ButtonArray = [String]()

    override func viewDidLoad() {
      super.viewDidLoad()

      Array =  ["1","2","3","4"]
      ButtonArray = ["1", "2", "3", "4"]

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

    }

    override func viewDidAppear(animated: Bool) {

    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Array.count
    }




    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell


        let Label = cell.viewWithTag(1) as! UILabel

        Label.text = Array[indexPath.row]

        let Button = cell.viewWithTag(2) as! UIButton

        Button.setTitle(ButtonArray[indexPath.row], forState: UIControlState.Normal)
      //  Button.addTarget(self,action:#selector(ButtonArray(_:)), forControlEvents:.TouchUpInside)
        Button.addTarget(self, action: Selector("ButtonArray:"), forControlEvents:.TouchUpInside)

        return cell







    }
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("Inside didSelectItemAtIndexPath")
    }

        func ButtonArray(sender : UIButton) {
            print("m")


            if sender.currentTitle == "1"{
            performSegueWithIdentifier("segueFirst", sender: nil)
            }

            if sender.currentTitle == "2"{

            }
            if sender.currentTitle == "3"{

            }
            if sender.currentTitle == "4"{

            }

     }
}

FirstCollectionViewController:
class FirstCollectionViewController : UIViewController {

    @IBAction func SegueFirstCollectionViewController(sender: UIButton) {
        performSegueWithIdentifier("SegueFirstCollectionViewController", sender: nil)
    }

}

最佳答案

你不需要把牢房带到外面

在中设定值

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
    //set value here
    if self.currentTitle == "1" ){
    //
    }
    else {
    //...
    }

}

并重新加载特定的单元格
let indexPath = IndexPath(item: i, section: 0)
self.tableView.reloadRows(at: [indexPath], with: .automatic)

希望能为您提供帮助:)

10-03 01:06