CollectionView不显示最后一节

时间:2017-02-17 09:26:48

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个自定义UICollectionView,总共有4(0,1,2,3)个部分。我正在做的是,如果没有信息显示section 2,那么我将此部分的单元格大小设置为CGSize(width: view.bounds.width, height: 0)。问题是来自第3部分的单元格也没有显示出来,甚至在CGSize(width: view.bounds.width, height: 50)的交换机sizeForItemAtIndexPath上调用了section 3

到目前为止我发现的是switch cellForItemAtIndexPath case 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() } } 中我错误的事情......也许你们其中一人之前遇到过这种情况。

以下是我的一些代码。

<form method="get" enctype="multipart/form-data">
  <input type="hidden" value="0" name="foo[]">
  <input type="checkbox" value="1" name="foo[]">
</form>

3 个答案:

答案 0 :(得分:0)

我不想显示一个部分,int numberOfItemsInSection方法,因为您的判别式(没有要显示的项目)应该为该部分返回0(零),cellForItemAtIndexPath永远不会要求那个部分。

细胞高度的变化不是正确的方法。

答案 1 :(得分:0)

为什么只有在第二部分中显示数据时才将numberOfSectionsInCollectionView方法更改为return 4,否则更改为return 3

通过这种方式,你不需要改变细胞的高度,这不是一个好的方法。

答案 2 :(得分: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