UICollectionView 最后一个单元格未对齐 [英] UICollectionView last cell not aligned

查看:34
本文介绍了UICollectionView 最后一个单元格未对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 新手,现在遇到了一个问题.

I am new to iOS and I now have an issue.

我的目标是获得一个带有过滤按钮的滑动条.我有一个 UICollectionView 作为其他视图中的子视图,除最后一个单元格外,其他所有视图都显示良好.

My goal is to get a sliding bar with filtering buttons on it. I have a UICollectionView as a subview in some other view all is displaying well except for the last cell.

这里是uicollectionview的delegate和datesource

Here is the delegate and datesource of the uicollectionview

class PmtCategoryCollectionViewController: UICollectionViewController {
var categories = ["Mediterranean", "Italian", "French", "Chinese", "American", "Indian"]

var buttonDistanceToEachOther: CGSize = CGSize(width: 20, height: 60)
var fontOnCategoryButton = UIFont.systemFontOfSize(15.0)

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    println(categories.count)
    return categories.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PmtCategoryCollectionViewCell

    // Configure the cell
    println(categories[indexPath.row])
    cell.category = categories[indexPath.row]
    return cell
}


// determine the size of each cell
func collectionView(collectionView: UICollectionView,
    layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        var text = categories[indexPath.row]
        var size: CGSize = NSString.sizeWithAttributes(text)([NSFontAttributeName:fontOnCategoryButton])
        size.height += buttonDistanceToEachOther.height
        size.width += buttonDistanceToEachOther.width
        return size

}

}

所有单元格的大小为:

Size is (118.91, 72.895)
Size is (37.52, 72.895)
Size is (60.77, 72.895)
Size is (67.025, 72.895)
Size is (75.5, 72.895)
Size is (84.545, 72.895)
Size is (61.745, 72.895)
Size is (176.21, 72.895) 

任何帮助都会很棒!

推荐答案

我遇到了同样的问题,在我的情况下,帮助只是从 UI Builder 中的 UICollectionView 中删除原型单元格项并使用 UICollectionViewCell xib 而不是通过创建 xib 并将其注册为 viewDidLoad 中某处的单元格视图笔尖:

I had the same issue, in my case what helped was simply remove prototype cell items from UICollectionView in UI Builder and use UICollectionViewCell xib instead by creating xib and registering it as a cell view nib somewhere in viewDidLoad:

[collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"];

以及稍后在 cellForItemAtIndexPath 中:

CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCellIdentifier" forIndexPath:indexPath];

这篇关于UICollectionView 最后一个单元格未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆