0

총 4 개의 (0, 1, 2, 3) 섹션이있는 사용자 지정 UICollectionView가 있습니다. 내가하고있는 일은 section 2을 표시 할 정보가 없으면이 섹션의 셀 크기를 CGSize(width: view.bounds.width, height: 0)으로 설정한다는 것입니다. 문제는 섹션 3의 셀이 심지어 CGSize(width: view.bounds.width, height: 50)section 3에 대한 스위치에 sizeForItemAtIndexPath에서 호출되었다고 생각하는 경우에도 표시되지 않습니다.CollectionView에 마지막 섹션이 표시되지 않습니다.

지금까지 발견 한 내용은 내 switchcellForItemAtIndexPathcase 2 and 3이이 경우에 호출되지 않습니다.

section 2에서 return 0까지의 셀을 설정하면 section 3이 표시된다는 것을 발견했습니다.

내가 실종되었다고 생각하는 것은 layout에 내가 잘못하고있는 것입니다 ... 어쩌면 당신 중 하나가 전에 이것을 경험했습니다.

다음은 제 코드입니다.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    switch indexPath.section { 
    case 0: return CGSize(width: view.bounds.width, height: 50) 
    case 1: return CGSize(width: view.bounds.width, height: 75) 
    case 2: 
     if let description = description , description.characters.count > 0 { 
      let sizingNibNew = NSBundle.mainBundle().loadNibNamed("BioCollectionViewCell", owner: BioCollectionViewCell.self, options: nil) 
      let sizingCellNew = sizingNibNew?[0] as! BioCollectionViewCell 

       sizingCellNew.myText = description 

      sizingCellNew.initialize() 

      return CGSize(width: view.bounds.width, height: sizingCellNew.myTextView.frame.height + 10) 
     } else { 
      return CGSize(width: view.bounds.width, height: 0) 
     } 
    case 3: 
     return CGSize(width: view.bounds.width, height: 50) 
    default: 
     return CGSize(width: view.bounds.width, height: 0) 
    } 
} 


func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 4 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    switch section { 
    case 0: return 4 
    case 1: return 1 
    case 2: return 1 
    case 3: return 2 
    default: 
     return 0 
    } 
} 


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    switch indexPath.section { 

    case 0: 
     switch indexPath.item { 
     case 0: 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MapView", forIndexPath: indexPath) as! MapView 

      if let address = address, let city = city { 
       cell.setAddressInMapView(address, cityAddress: city) 
      } 

      return cell 
     case 1: 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("TitleCell", forIndexPath: indexPath) as! TitleCell 

      cell.titleLabel.text = title1 

      return cell 

     case 2: 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell 

      cell.nameLabel.text = xName 

      return cell 
     case 3: // address 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AddressCell", forIndexPath: indexPath) as! AddressCell 

      cell.addressLabel.text = address 

      return cell 
     default: 
      return UICollectionViewCell() 
     } 

    case 1: 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Name", forIndexPath: indexPath) as! ShowFeaturedArtist 

     cell.xId = id ?? "" 
     cell.nameLabel.text = name ?? "" 

     cell.populateViewWithoutQuery() 

     return cell 

    case 2: 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BioCollectionViewCell", forIndexPath: indexPath) as! BioCollectionViewCell 

     if let description = description where description.characters.count > 0 { 
      cell.myText = description 
     } 
     cell.initialize() 

     return cell 

    case 3: 
     switch indexPath.item { 
     case 0: 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserAttending", forIndexPath: indexPath) as! UserAttending 

      cell.label.text = "0 " + " Users Attending" 
      return cell 

     case 1: 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserAttending", forIndexPath: indexPath) as! UserAttending 

      cell.label.text = "Release Dates" 
      return cell 

     default: 
      return UICollectionViewCell() 

     } 

    default: 
     return UICollectionViewCell() 
    } 
} 
+0

이슈 무엇입니까? 'section-2'에 표시 할 데이터가 없으므로 높이를 0으로 놓고'cellAtItem'은 그걸 ok라고 부르지 않습니다. 'height = 0'을 넣으면 컴파일러가 무엇을 기대하는지 ........ CPU주기를 낭비하기 위해'cellForItem'을 실행합니다. –

+0

문제는 '섹션 2'에 데이터가 없으면 섹션 3이 표시되지 않는다는 것입니다. – John

+0

섹션 3이 섹션 2에오고 콜렉션 뷰를 다시로드하는 방식으로 데이터 소스를 만듭니다. – Koushik

답변

0

numberOfItemsInSection에서이 작업을 수행하여 수정 :

switch section { 
    case 0: return 6 
    case 1: return 1 
    case 2: 
     if show._description?.characters.count > 0 { 
      return 1 
     } else { 
      return 0 
     } 
    case 3: return 2 
+1

정확합니다. 당신이해야 할 일. –

+0

사실이지만 섹션의 크기가 0 인 항목도 다른 섹션에 영향을주지 않아야합니다. – John

0

나는 당신이 섹션을 보여 그 부분에 대해 0 (영)을 반환해야 당신의 판별 (표시 할 항목)에 대한 numberOfItemsInSection 방법을 int로하지 않으며, cellForItemAtIndexPath 해당 섹션을 요구하지 않습니다 .

세포의 높이 변화는 올바른 방법이 아닙니다.

0

두 번째 섹션에 표시 할 데이터가있는 경우에만 numberOfSectionsInCollectionView 메서드를 return 4으로 변경하고 그렇지 않은 경우 return 3으로 변경 하시겠습니까?

이렇게하면 셀의 높이를 변경할 필요가 없어 어쨌든 좋은 방법이 아닙니다.