【发布时间】:2015-09-28 08:12:59
【问题描述】:
如何使 UICollectionView 自动布局,以便根据项目总数决定某个时刻出现在屏幕上的项目数。我实际上想做这样的布局,如果我有 9 个项目,那么所有 9 个项目都应该同时显示在集合视图上。如果项目是 8 或其他,则项目的大小应该以这样的方式增加,以使它们仍然占用集合视图的整个区域。
我绘制的条件如下:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell = collectionView.cellForItemAtIndexPath(indexPath) as CollectionViewCell
if((cell.selected) && (indexPath.row%2 == 0))
{
cell.backgroundColor = UIColor.whiteColor()
imageview.image = UIImage( named: a[indexPath.row])
}
else if((cell.selected) && (indexPath.row%2 != 0))
{
cell.backgroundColor = UIColor.whiteColor()
imageview.image = UIImage( named: a[indexPath.row])
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var abc : CGSize!
if(a.count == 1){
abc = CGSize(width: self.view.frame.width, height: self.view.frame.height/2)
}
else if(a.count == 2) {
abc = CGSize(width: self.view.frame.width/2, height: self.view.frame.height/2)
}
else if (a.count > 2) && (a.count<6){
abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/2)
}
else {
abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/5)
}
return abc
}
func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return a.count
}
func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
【问题讨论】:
标签: ios swift autolayout uicollectionview