[英]UIcollectionView Spacing
我有10个单元格,其中第6个单元格的宽度必须与其他单元格不同。我尝试在流委托方法中对其进行更改。 但是从第7个单元格到第10个单元格的间距出了问题。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let widthOfCollectionView = collectionView.bounds.width - 40;
// For collectionView use "item" instead of "row"
if indexPath.item == 6{
return CGSizeMake(widthOfCollectionView,100)
}else{
return CGSizeMake(widthOfCollectionView/3, 100)
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10;
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 10;
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
return UIEdgeInsetsMake(5, 5, 5, 5);
}
我对您的代码进行了一些更改,如下所示:
import UIKit
class ViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var sampleCollectionView: UICollectionView!
let reuseIdentifier = "cell"
let insetValue: CGFloat = 5.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
// Use the outlet in our custom class to get a reference to the UILabel in the cell
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if indexPath.item == 6{
return CGSizeMake(collectionView.bounds.width - insetValue * 2,100)
}else{
return CGSizeMake(floor((collectionView.bounds.width - insetValue * 4)/3), 100)
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return insetValue;
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return insetValue;
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
return UIEdgeInsetsMake(insetValue, insetValue, insetValue, insetValue);
}
}
最终输出:
要下载示例项目,请使用以下链接:
对于小物件的宽度,您应该返回(collectionView.bounds.width - 30)/3
,左插图为5,右插图为5,间距为2 * 10 = 30。
对于大项目的宽度,您应该返回collectionView.bounds.width - 10
,左侧为5,右侧为5 = 10。
你可以代替这个
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let widthOfCollectionView = collectionView.bounds.width - 40;
if indexPath.item == 6{
return CGSizeMake(widthOfCollectionView,100)
}else{
return CGSizeMake(widthOfCollectionView/3, 100)
}
}
用
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let smallItemWidth = floor((collectionView.bounds.width - 30)/3 * 1000) / 1000
let largeItemWidth = collectionView.bounds.width - 10
if indexPath.item == 6{
return CGSizeMake(largeItemWidth, 100)
}else{
return CGSizeMake(smallItemWidth, 100)
}
}
*使用floor()舍入大数位小数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.