UICollectionView last cell not aligned

不问归期 提交于 2020-01-23 07:54:05

问题


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

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.

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

}

}

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!


回答1:


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"];

and later in cellForItemAtIndexPath:

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



回答2:


Sometimes, based on my own experience, iOS don't align properly the last cell on a 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.



来源:https://stackoverflow.com/questions/31146480/uicollectionview-last-cell-not-aligned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!