2017-05-06 1 views
0

A avoir un bouton dans une collectionviewcell et quand je clique sur le bouton, l'objectif est de segue à un contrôleur de vue plus détaillée.cliquez sur le bouton dans la cellule d'uicollection à segue

class WelcomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,UIGestureRecognizerDelegate, UISearchBarDelegate { 


    @IBOutlet weak var CollectionView: UICollectionView! 


    var databaseRef = FIRDatabase.database().reference() 
    var loggedInUser = FIRAuth.auth()?.currentUser 
    var dictDetails: [String:AnyObject]? 
    var posts = NSMutableArray() 
    let storage = FIRStorage.storage() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     (CollectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize = CGSize(width: (self.view.frame.width - 10)/2.4, height: (self.view.frame.width - 10)/1.5) 
     self.navigationItem.title = "Lit Swap" 
     CollectionView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 300) 
     self.CollectionView.contentInset = UIEdgeInsetsMake(-65, 0, 0, 0) 
     definesPresentationContext = true 

     loadData() 
    } 


    @IBAction func editButtonTapped() -> Void { 
     print("Hello Edit Button") 
     performSegue(withIdentifier: "UsersProfile", sender: self) 

     func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
      if segue.identifier == "UsersProfile" { 


       self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 


       if let indexPaths = self.CollectionView!.indexPathsForSelectedItems{ 

       ///I DONT THINK YOU SHOULD REFERENCE POST, REFERENCE BOOKS INSTEAD/////////////// 

        // if let indexPaths = self.CollectionView.cellForItem(at: <#T##IndexPath#>){ 


        let vc = segue.destination as! UsersProfileViewController 
        let cell = sender as! UICollectionViewCell 
        let indexPath = self.CollectionView!.indexPath(for: cell) 
        let post = self.posts[(indexPath?.row)!] as! [String: AnyObject] 
        // let username = post["username"] as? String 
        let userpicuid = post["uid"] as? String 
        // vc.username = username 
        vc.userpicuid = userpicuid 


        print(indexPath?.row) 

         }} } 


    } 


    func loadData(){ 
     if (FIRAuth.auth()?.currentUser) != nil{ 
      FIRDatabase.database().reference().child("books").observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in 

       let loggedInUserData = snapshot 
       if let postsDictionary = snapshot .value as? [String: AnyObject] { 
        for post in postsDictionary { 
         self.posts.add(post.value) 
        } 
        self.CollectionView.reloadData() 


       }})} 
    } 

    // Properties of the UICollectionView 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 
     return self.posts.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 



     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PinterestLikeCollectionViewCell 


     //configure the cell... 

     if (FIRAuth.auth()?.currentUser) != nil{ 


      print(posts[indexPath.row]) 
      let post = self.posts[indexPath.row] as! [String: AnyObject] 
      cell.Title.text = post["title"] as? String 
      cell.Author.text = post["Author"] as? String 

      let editButton = UIButton(frame: CGRect(x: 8, y: 225, width: 154, height: 45)) 

      editButton.addTarget(self, action: #selector(editButtonTapped), for: UIControlEvents.touchUpInside) 
      editButton.tag = indexPath.row 
      print(indexPath.row) 
      editButton.isUserInteractionEnabled = true 

      cell.addSubview(editButton) 

      if let imageName = post["image"] as? String { 

       let imageRef = FIRStorage.storage().reference().child("images/\(imageName)") 


       imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in 
        if error == nil { 

         let image = UIImage(data: data!) 
         cell.Books.image = image 

         cell.Books.roundCornersForAspectFit(radius: 10) 
         cell.Books.clipsToBounds = true 


        }else { 

         print("Error downloading image:") 
        }})}} 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     return CGSize(width: CGFloat((collectionView.frame.size.width/5) - 20), height: CGFloat(500)) 
    } 


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    } 



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "details" { 


      self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 


      if let indexPaths = self.CollectionView!.indexPathsForSelectedItems{ 

       let vc = segue.destination as! BookDetailsViewController 
       let cell = sender as! UICollectionViewCell 
       let indexPath = self.CollectionView!.indexPath(for: cell) 
       let post = self.posts[(indexPath?.row)!] as! [String: AnyObject] 
       let Booked = post["title"] as? String 
       let Authors = post["Author"] as? String 
       let ISBNS = post["ISBN"] as? String 
       let Prices = post["Price"] as? String 
       let imageNames = post["image"] as? String 
       let imagesTwo = post["imageTwo"] as? String 
       let imagesThree = post["imageThree"] as? String 
       let imagesFour = post["imageFour"] as? String 
       let imagesFive = post["imageFive"] as? String 
       vc.Booked = Booked 
       vc.Authors = Authors 
       vc.ISBNS = ISBNS 
       vc.Prices = Prices 
       vc.imageNames = imageNames 
       vc.imagesTwo = imagesTwo 
       vc.imagesThree = imagesThree 
       vc.imagesFour = imagesFour 
       vc.imagesFive = imagesFive 

       print(indexPath?.row) 

      } }} 

} 

Cependant, il ne fait rien. J'ai actuellement ma configuration de cellule que lorsque vous cliquez dessus, il se relie à un contrôleur de vue détaillée. Mais le bouton dans la cellule, lorsqu'il est cliqué devrait aller à un autre contrôleur de vue détaillée. Pour la section de la cellule, les informations à saisir ce que mettre dans la fonction didSelectItemAt. Je ne suis pas sûr de la fonction d'inclure le segue pour le bouton dans la cellule.

Répondre

0

Pouvez-vous inclure votre classe dans son ensemble afin que je puisse mieux comprendre comment vous configurez les vues? Je vais mettre à jour ma réponse avec ce que je pense aiderait, à partir de ce que je pense qu'il y a quelques choses différentes qui pourraient arriver à cause de cela.

MISE À JOUR

Essayez ceci:

  1. Créer et ajoutez votre bouton dans votre classe PinterestLikeCollectionViewCell.
  2. Créez un protocole qui dira à son délégué de préformer votre action lorsque vous cliquerez sur le bouton de la cellule.
  3. Affectez votre classe WelcomeViewController en tant que délégué et conformez-vous au protocole.
+0

Je l'ai mis à jour – juelizabeth