2017-10-10 133 views
0

我正在开发IOS App.Click到Tableview单元格比值显示在collectionViewCell上,但我希望根据单元格更改collectionView高度。根据单元格的不更改单元格高度动态更改UICollectionView高度

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    return CGSizeMake(_collectionView.frame.size.width , _collectionView.frame.size.height+30); 

} 
+0

你想改变集合视图高度或单元格高度? – ChanWarde

+0

你想改变基于细胞数量的collectionView高度吗? –

+0

是根据细胞数改变collectionview的高度 –

回答

0

您需要添加该代码

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 
{ 
    var numberOfCellInRow : Int = yourArray.count 
    var padding : Int = 5 
    var collectionCellWidth : CGFloat = (self.view.frame.size.width/CGFloat(numberOfCellInRow)) - CGFloat(padding) 
    return CGSize(width: collectionCellWidth , height: collectionCellWidth) 
} 
0

您可以通过使用它的滚动视图 的contentSize的的CollectionView被加载后更改它..如果你更新任何单元格中插入项目或删除您需要的物品更新再次 高度即

myCollectionView.reload() 

现在更新帧或高度

//if you are using auto-resizing you can update frame 
let frame = myCollectionView.frame 
frame.height = myCollectionView.contentSize.height 
myCollectionView.frame = frame 

,如果您正在使用自动布局品牌和IBOutlet中为您的CollectionView的高度约束

@IBOutlet collectionViewHeightConstraint:NSLayoutContraint! 

现在更新这个contraint

collectionViewHeightConstraint.constant = myCollectionView.contentSize.height 
相关问题