2017-02-12 2 views
0

UI 업데이트를 수행 할 수 있도록 다른 collectionView 안에 중첩 된 collectionView에 대한 참조를 가져와야합니다. 내의 ViewController 클래스에서참조가없는 viewController에서 중첩 된 collectionView에 액세스하십시오.

나는 "외부"collectionView에 대한 출구를 가지고 : 나는 willDisplayCell에서 "내부"collectionView에 대한 데이터 소스 및 위임 설정

@IBOutlet var outerCollectionView: UICollectionView! 

: 다음

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
    guard let outerViewCell = cell as? OuterCollectionCell else { return } 
    outerViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forItem: indexPath.item) 
       outerViewCell.innerCollectionView.reloadData() 
       outerViewCell.innerCollectionView.layoutIfNeeded() 
} 

를 내가 cellForItemAt의 두 컬렉션 뷰에 대해 셀을 채울 수 있습니다.

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

    if collectionView == self.outerCollectionView { 
     let outerCell: OuterCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierOuter, for: indexPath) as! OuterCollectionCell 

     outerCell.labelCell.text = self.packArray[indexPath.item].title 

     // other stuff.... 

     return outerCell 

    } else {// innerCollectionView 
     let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell 

     innerCell.imageCell.file = self.multiPartArray[collectionView.tag][indexPath.item].image 

     // other stuff.... 

     return innerCell 
    } 
} 

ECT (청색 인 0)

enter image description here

collectionView.tag은 "내부"collectionView의 인스턴스

: 이것은 다음과 같은 결과를 생성한다.

이제 셀에서 UI 업데이트를 수행하고 싶습니다. 다양한 이유로 cellForItemAt를 사용할 수 없습니다. 그러나

if let editCell = collectionView.cellForItem(at: indexPath) as? InnerCollectionCell { 
    editCell.imageCell.image = UIImage(named: "test") 
} 

내가 그것을 이러한 UI 업데이트가 수행되는 시간에 의해 재사용되었을 수 있기 때문에 내가이없는 "내부"collectionView에 대한 참조가 필요 이렇게 : 그래서 내가 이전에했던 것은 같은 것이있다 . 그러나 내가하는 일은 collectionView.tag인데, 이는 Int 참조와 업데이트를 수행하려는 셀의 indexPath입니다.

collectionView 내에서 지정된 셀의 UI를 직접 참조 할 필요가 없습니까? 아니면이 간단한 생각입니까?

나는 어떤 종류의 델리게이트 함수로 나는 어차피 어떻게 생각해야 하는가. 또한 아래에 참조하기 위해 내 OuterCollection 클래스에서 "내부"collectionView를 정의하고 데이터 소스와 위임을 설정합니다.

class OuterCollectionCell: UICollectionViewCell { 

@IBOutlet var labelCell: UILabel! 
// define the smaller collection view that is in the cell 
@IBOutlet var innerCollectionView: UICollectionView! 

// set delegate and datasource for new collectionView 
// this is download collection manager 
func setCollectionViewDataSourceDelegate 
    <D: UICollectionViewDataSource & UICollectionViewDelegate> 
    (dataSourceDelegate: D, forItem item: Int) { 

    innerCollectionView.delegate = dataSourceDelegate 
    innerCollectionView.dataSource = dataSourceDelegate 
    innerCollectionView.tag = item 
    innerCollectionView.reloadData() 
    innerCollectionView.layoutIfNeeded() 

} 
} 

답변

1

OuterCollectionCell 클래스에 정의 된 innercollectionview의 대리자 및 데이터 소스 메서드를 가질 수 있습니다.

 // Set the delegate and datasource in your custom cell class as below:- 
     class OuterCollectionCell: UITableViewCell { 
     // set the delegate & datasource of innercollectionView in the init or 
     // awakefromnib or storyboard. 
     // I have set it in storyboard and it works for me. 
     // Implement the datasource and delegate methods of innercollectionView in this class. 
     } 

이 방법 innerCollectionView의 위임 및 데이터 소스가 outercollectionview 분리 될 것입니다 그리고 당신은 OuterCollection 세포 클래스에 innecrCollectionView의 모든 로직을 처리 할 수 ​​- :이보십시오.

+0

나는 이것을 할 수 있는지 몰랐다. 작동해야하는 것 같습니다. – Pippo

+0

내 프로젝트에서이 작업을 수행했습니다. – iAviator