0

내 UICollectionView를 업데이트하는 데 문제가 있습니다. 중첩 된 collectionViews로 구성된 다음 설치 프로그램이 있습니다.중첩 된 UICollectionView 업데이트 - Swift 3

enter image description here

homeControllerView 활성화 페이징 상기 CollectionView 1와 collectionViewController이다. CollectionView 1의 모든 셀은 그 자체의 collectionView입니다 (예 : CollectionView A, B, ...이 이유는 모든 CollectionView A, B, ...는 피드이며 셀 A1, A2, B1 ...으로 표시되는 정보를 가져옵니다.

HomeViewController가로드되고 CollectionViews A, B ...가 채워지면 모든 (주) 셀 1, 2가 정보를 가져 오기 시작합니다. 피드 중 일부는 로그인 한 현재 사용자의 개인 정보를 가져 오는 것입니다.

다른 사용자로 전환 한 후 (기본) collectionView를 업데이트하여 모든 셀 1 , 2, ...가 정보를 다시 가져 오기 시작했습니다. 그래서 내 LoginController 내에서 호출

self.homeController?.collectionView?.reloadData() 

그러나 이것은 현재 보이는 셀에만 작동하는 것 같습니다. 표시된 예제의 셀 2. 또한 다시로드 항목 만 볼 수 있습니다 그 사람들을 위해 일 :

self.homeController?.collectionView?.reloadItems(at: (self.homeController?.collectionView?.indexPathsForVisibleItems)!) 

을 내 homeViewController에서 나는이 같은 cellForItem 함수를 재정의이

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    switch indexPath.item { 
     case 0: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: incomingFeedCellId, for: indexPath) as! IncomingFeedCell 
      return cell 
     case 1: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: popularFeedCellId, for: indexPath) as! PopularFeedCell 
      return cell 
     case 2: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: favoritFeedCellId, for: indexPath) as! FavoritFeedCell 
      return cell 
     case 3: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: personalFeedCellId, for: indexPath) as! PersonalFeedCell 
      return cell 
     default: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: incomingFeedCellId, for: indexPath) as! IncomingFeedCell 
      return cell 
    } 
} 

그러나 .reloadData() cellForItem 기능이 있으므로 한 번 시작, 다시 호출되지 않습니다와 , 셀이 다시 업데이트되지 않습니다. 셀이 대기열 때

func fetch(){ 
    posts = [Post]() 
    let ref = FIRDatabase.database().reference() 

    ref.child("posts_incoming").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in 
     if let dictionary = snapshot.value as? NSDictionary { 
      let post = Post() 
      post.imageUrl = dictionary["image_url"] as? String 
      post.caption = dictionary["caption"] as? String 
      ... 
      ... 
      self.posts?.append(post) 

      DispatchQueue.main.async(execute: { 
       self.collectionView.reloadData() 
      }) 
     } 
    }, withCancel: nil) 
} 

가 현재 나는의 (주) 내에서 사용자 ID를 추적하므로 세포를 유지 :

모든 FeedCell 적어도 한 번 세포가 생성된다라고 페치() 함수를 가지고 (예 : 다음 피드로 스 와이프) uid가 변경된 경우이를 검사합니다. 그렇다면 fetch가 다시 실행됩니다. 그러나이 문제에 대한보다 우아한 해결책이 분명 있어야합니다. 모든 셀을 버리고 각 셀에 대해 새 초기화를 수행하는 방법이 있습니까?

답변

0

UICollectionView의 맞춤 클래스를 만드는 것이 좋으며 모든 피드 가져 오기 호출은 맞춤 UICollectionView 클래스에서 이루어져야합니다. 페이징에 대한

홈페이지 UICollectionView와 UICollectionView의 세포 내에서 jQuery과 추가 또한 UICollectionViews 중첩되지처럼 사용할 수있는, 간단하게합니다. 당신을 할당 할 수있는 UIViewController

또한 당신이 dequeueReusableCell 항상 그렇지 않으면 너무 많은 메모리를 사용해야합니다

jQuery과 UICollectionView에서 사용자 정의 클래스에

UICollectionView :

는 그래서 다시로드 같은 것 앱이 다운되었습니다.

+0

답변을 주셔서 감사합니다. 그러나 이것이 어떻게 도움이 될 수 있는지 이해하지 못합니다.CollectionView 1을 셀을 업데이트 할 새로운 함수가있는 사용자 정의 CollectionView로 만들면 아직 보이지 않는 셀에 액세스 할 수 없습니다. –

+0

예, 5 개의 수퍼 셀을 고정 시켰습니다. 한 가지만 할 수 있습니다. 콘텐츠 크기가 뷰의 5 배인 사용자 ScrollView입니다. 그런 다음 각 containerView에 UIViewController를 분리 한 5 개의 컨테이너 뷰를 추가하십시오. 이렇게하면 모든 것을 쉽게 관리 할 수 ​​있습니다. – ankit

관련 문제