2016-12-27 11 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

didselect 메서드에 중단 점을 넣습니다. 셀을 탭하면 chck가 호출되지 않습니다. –

+0

셀을 탭하면 메서드가 호출되지 않으므로 아무 것도하지 않습니다. –

+0

업데이트 cellForRowAtIndexPath 메서드 –

답변

0

내가 createCollectionView 기능에 view.gestureRecognizers?.removeAll()을 추가하여 고정합니다.

관련 문제