简体   繁体   English

UICollectionView最后一个单元格未对齐

[英]UICollectionView last cell not aligned

I am new to iOS and I now have an issue. 我是iOS的新手,现在我遇到了一个问题。

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作为其他视图中的子视图,除了最后一个单元格外,它们都显示良好。

uicollectionview的显示

Here is the delegate and datesource of the 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

}

} }

The size of all the cells are: 所有单元格的大小为:

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) 

Any help would be great! 任何帮助都会很棒!

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 : 我有同样的问题,在我的情况下,帮助只是从UI Builder中的UICollectionView中删除原型单元格项目并使用UICollectionViewCell xib而不是通过创建xib并将其注册为viewDidLoad中的某个单元格视图nib:

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

and later in cellForItemAtIndexPath : 然后在cellForItemAtIndexPath中

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

Sometimes, based on my own experience, iOS don't align properly the last cell on a UICollectionView. 有时,根据我自己的经验,iOS无法正确对齐UICollectionView上的最后一个单元格。

To avoid that, you can add a new cell at last position on your dataSource, and set its height as 0. With that 'solution', the wrong-aligned cell will be that last cell that is not important for you and, in practise, the cell will be invisible. 为避免这种情况,您可以在dataSource的最后位置添加一个新单元格,并将其高度设置为0.使用该“解决方案”,错误对齐的单元格将是对您来说不重要的最后一个单元格,并且在实践中,细胞将是隐形的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM