2016-12-27 9 views
1

UICollectionViewでセルを選択しようとしていますが、機能しません。私は多くのチュートリアルを見てきましたが、助けてくれなかったので、私はまったく同じでしたが、私はうまくいきません。私は誰かが私を助けることを願っていますCollectionViewの選択が機能しない

CollectionViewコードを作成します。

func createCollectionView() { 
    let layout = UICollectionViewFlowLayout() 
    layout.itemSize = CGSize(width: view.frame.size.width/2.75, height: view.frame.size.width/2.75) 
    layout.minimumLineSpacing = 10 
    layout.scrollDirection = .vertical 

    photoCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width/1.2, height: view.frame.size.height - biographyLabel.center.y - 75 - 
     navigationController!.navigationBar.frame.size.height), collectionViewLayout: layout) 
    photoCollectionView.backgroundColor = UIColor.white 
    photoCollectionView.showsVerticalScrollIndicator = false 
    photoCollectionView.showsHorizontalScrollIndicator = false 
    photoCollectionView.allowsSelection = true 
    photoCollectionView.delegate = self 
    photoCollectionView.dataSource = self 
    photoCollectionView.register(PhotosCollectionViewCell.self, forCellWithReuseIdentifier: "photoCell") 
    photoCollectionView.center = CGPoint(x: view.frame.size.width/2, y: view.frame.size.height - (photoCollectionView.frame.size.height/2) - navigationController!.navigationBar.frame.size.height) 

    view.addSubview(photoCollectionView) 
} 

選択コード:助けるため

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    print("test") 
} 

ありがとう!

UPDATE:

は、細胞のコードを作成します。

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    if(scrollView.contentOffset.y <= 0) { 
     if(scrollView.frame.size.height == view.frame.size.height - navigationController!.navigationBar.frame.size.height - tabBarController!.tabBar.frame.size.height) { 
      UIView.animate(withDuration: 0.5, animations: { 
       self.coverImage.center = CGPoint(x: self.view.frame.size.width/2, y: self.navigationController!.navigationBar.frame.size.height + (self.coverImage.frame.size.height/2)) 
       self.profileImage.center = CGPoint(x: self.coverImage.center.x, y: self.coverImage.center.y + (self.coverImage.frame.size.height/2)) 
       self.verifiedLogo.center = CGPoint(x: self.profileImage.center.x + (self.profileImage.frame.size.width/3), y: self.profileImage.center.y - (self.profileImage.frame.size.height/3)) 
       self.usernameLabel.center = CGPoint(x: self.view.frame.size.width/2, y: self.profileImage.center.y + (self.profileImage.frame.size.height/2) + (self.usernameLabel.frame.size.height/2)) 
       self.biographyLabel.center = CGPoint(x: self.view.frame.size.width/2, y: self.usernameLabel.center.y + (self.usernameLabel.frame.size.height/2) + (self.biographyLabel.frame.size.height/2)) 
       self.biographyTextfield.center = self.biographyLabel.center 
       self.biographyCharactersLabel.center = CGPoint(x: self.view.frame.size.width/2, y: self.biographyTextfield.center.y + self.biographyCharactersLabel.frame.size.height) 
       self.editImagesLogo.center = CGPoint(x: self.view.frame.size.width - self.editImagesLogo.frame.size.width, y: self.navigationController!.navigationBar.frame.size.height + (self.editImagesLogo.frame.size.height * 2)) 
       self.editBiographyImage.center = CGPoint(x: self.view.frame.size.width - (self.editBiographyImage.frame.size.width * 1.5), y: self.biographyLabel.center.y) 
       self.editImagesView.center = CGPoint(x: self.editImagesLogo.center.x + (self.editImagesLogo.frame.size.width/2) - (self.editImagesView.frame.size.width/2), y: self.editImagesLogo.center.y + (self.editImagesLogo.frame.size.height/2) + (self.editImagesView.frame.size.height/2)) 
       self.doneImage.center = self.editBiographyImage.center 
       self.nextImage.center = CGPoint(x: self.view.frame.size.width - (self.nextImage.frame.size.width/1.5), y: self.view.frame.size.height - (self.nextImage.frame.size.height/1.25) - 
        self.navigationController!.navigationBar.frame.size.height) 

       self.photoCollectionView.frame.size.height = self.view.frame.size.height - self.biographyLabel.center.y - 75 - self.navigationController!.navigationBar.frame.size.height 
       self.photoCollectionView.center = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height - (self.photoCollectionView.frame.size.height/2) - self.navigationController!.navigationBar.frame.size.height) 
      }, completion: { (true) in 
       self.navigationItem.setHidesBackButton(false, animated: false) 

       for barButtons in self.tabBarController!.tabBar.items! { 
        barButtons.isEnabled = true 
       } 
      }) 
     } 
    }else{ 
     if(scrollView.frame.size.height == self.view.frame.size.height - self.biographyLabel.center.y - 75 - self.navigationController!.navigationBar.frame.size.height) { 
      navigationItem.setHidesBackButton(true, animated: false) 

      for barButtons in tabBarController!.tabBar.items! { 
       barButtons.isEnabled = false 
      } 

      UIView.animate(withDuration: 0.5, animations: { 
       self.coverImage.center.y = -(self.coverImage.frame.size.height/2) 
       self.profileImage.center.y = -(self.profileImage.frame.size.height/2) 
       self.verifiedLogo.center.y = -(self.verifiedLogo.frame.size.height/2) 
       self.usernameLabel.center.y = -(self.usernameLabel.frame.size.height/2) 
       self.biographyLabel.center.y = -(self.biographyLabel.frame.size.height/2) 
       self.biographyTextfield.center.y = -(self.biographyTextfield.frame.size.height/2) 
       self.doneImage.center.y = -(self.doneImage.frame.size.height/2) 
       self.biographyCharactersLabel.center.y = -(self.biographyCharactersLabel.frame.size.height/2) 
       self.editImagesLogo.center.y = -(self.editImagesLogo.frame.size.height/2) 
       self.editBiographyImage.center.y = -(self.editBiographyImage.frame.size.height/2) 
       self.editImagesView.center.y = -(self.editImagesView.frame.size.height/2) 
       self.nextImage.center.y = -(self.nextImage.frame.size.height/2) 

       self.photoCollectionView.frame.size.height = self.view.frame.size.height - self.navigationController!.navigationBar.frame.size.height - self.tabBarController!.tabBar.frame.size.height 
       self.photoCollectionView.center = self.view.center 
      }) 
     } 
    } 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if(photoLinks.isEmpty) { 
     photoCollectionView.isHidden = true 

     return 0 
    }else{ 
     photoCollectionView.isHidden = false 

     return photoLinks.count 
    } 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = photoCollectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotosCollectionViewCell 

    cell.awakeFromNib() 

    cell.photoImage.image = nil 

    if(loadedPhotos.count == photoLinks.count) { 
     cell.photoImage.image = loadedPhotos[indexPath.row] 
    }else{ 
     let photoLink = photoLinks[indexPath.row] 

     let url = URL(string: photoLink) 
     URLSession.shared.dataTask(with: url!) { (data, response, error) in 

      if(error != nil){ 
       print(error as Any) 
       return 
      } 

      DispatchQueue.main.async { 
       if let downloadedImage = UIImage(data: data!) { 
        let image = downloadedImage 

        cell.photoImage.image = image 
        self.loadedPhotos.append(image) 
       } 
      } 
     }.resume() 
    } 

    return cell 
} 
+0

そこにセレクトメソッドを置いて、セルをタップするとチャンクではない –

+0

セルをタップするとメソッドが呼び出されないので何もしません。 –

+0

update cellForRowAtIndexPathメソッド –

答えて

0

を私はcreateCollectionView機能でview.gestureRecognizers?.removeAll()を追加することによって、それを修正。

関連する問題