Ios UICollectionView最后一个单元格未对齐

Ios UICollectionView最后一个单元格未对齐,ios,swift,uicollectionviewcell,uicollectionviewlayout,Ios,Swift,Uicollectionviewcell,Uicollectionviewlayout,我是iOS新手,现在有个问题 我的目标是得到一个带有过滤按钮的滑动条。 我有一个UICollectionView作为其他视图中的子视图,除了最后一个单元格外,所有视图都显示得很好 以下是uicollectionview的委托和日期源 class PmtCategoryCollectionViewController: UICollectionViewController { var categories = ["Mediterranean", "Italian", "French", "Chi

我是iOS新手,现在有个问题

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

以下是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中删除原型单元格项,并通过创建xib并将其注册为viewDidLoad中某个单元格视图nib来使用UICollectionViewCell xib:

稍后在cellForItemAtIndexPath中:


有时,根据我自己的经验,iOS无法正确对齐UICollectionView上的最后一个单元格


为了避免这种情况,您可以在数据源的最后一个位置添加一个新单元,并将其高度设置为0。使用该“解决方案”,错误对齐的单元格将是对您不重要的最后一个单元格,实际上,该单元格将不可见。

您的最后一个函数将调整每个单元格的大小。你能每次打印出尺寸吗(作为一种健康检查)?Do println(“大小为”+大小)由于@acgressor,增加了单元格的大小。我将collectionView的高度改大了,似乎解决了这个问题。所以我猜当单元格的高度大于UICollectionView的高度时,会发生一些奇怪的渲染。有没有原因让你有不同的高度?你的上一个高度很大,难怪它会下降。您的单元格应具有相同的高度。第二项(72.895)是高度,第一项是宽度。所以我认为所有的高度实际上都是一样的。谢谢回复!
[collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"];
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCellIdentifier" forIndexPath:indexPath];