Objective c 更改UICollectionView中每个部分的插入
我用Swift编写了一个简单的Objective c 更改UICollectionView中每个部分的插入,objective-c,swift,uicollectionview,collectionview,sections,Objective C,Swift,Uicollectionview,Collectionview,Sections,我用Swift编写了一个简单的UICollectionView。我的SectionInsetts过去是0,35,0,35,这使整个collectionView页边距从左到右。我的minimumLineSpacing设置为默认值10,这意味着我的所有单元格彼此之间的边距都为10。一切都好 然后我将UICollectionView分为两个部分。第一部分用于保存一个单元格,该单元格作为创建新单元格的提示(将在第2部分结束)。第二部分保存所有其他单元格(实际内容) 我的问题是,自从我添加了第二节,第1节
UICollectionView
。我的SectionInsetts过去是0,35,0,35,这使整个collectionView页边距从左到右。我的minimumLineSpacing设置为默认值10,这意味着我的所有单元格彼此之间的边距都为10。一切都好
然后我将UICollectionView
分为两个部分。第一部分用于保存一个单元格,该单元格作为创建新单元格的提示(将在第2部分结束)。第二部分保存所有其他单元格(实际内容)
我的问题是,自从我添加了第二节,第1节中的一个静态单元格和第2节中的第一个单元格之间的间距不再是10,而是35+10。我现在试着把这个特殊的间距恢复到10
我的想法是添加一个if条件来确定它是第1节还是第2节,并相应地设置sectionInsets。然而,我被困在哪里以及如何确切地做到这一点。我似乎无法从视图控制器(例如cellForItemAtIndexPath)调用sectionInsets或FlowLayout。我应该在CustomCollectionViewFlowLayout子类中执行此操作吗?这是我在里面的代码
- (void)awakeFromNib
{
self.itemSize = CGSizeMake(305.0, 407.0);
self.minimumInteritemSpacing = 20.0;
self.minimumLineSpacing = 10.0;
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(0, 35, 0, 35);
}
这是我的视图控制器中我的部分的代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let firstCell = collectionView.dequeueReusableCellWithReuseIdentifier("createCell", forIndexPath: indexPath) as! CreateCollectionViewCell
firstCell.imageView.image = UIImage(named: "puppy3")
return firstCell
} else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mainCell", forIndexPath: indexPath) as! MainCollectionViewCell
cell.imageView?.image = self.imageArray[indexPath.row]
return cell
}
}
更新:
我想出来了。它为我的视图控制器中的UIEdgeInsets类型的sectionInsets添加两个变量,然后添加以下函数:
//Set custom insets per section
func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
if section == 0 {
return sectionInsets1
} else {
return sectionInsets2
}
}