4

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.

The display of the uicollectionview

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!

BersaKAIN
  • 303
  • 2
  • 3
  • 11
  • 1
    Your last function does the sizing of each cell. Can you print out the size each time (as a sanity check)? Do println("Size is " +size) – Aggressor Jun 30 '15 at 19:17
  • Thanks to @Aggressor, added the size of the cells. I changed the height of the collectionView to be larger and it seems to solve the issue. So I guess its when the height of the cell is larger than the height of the UICollectionView, some strange rendering will happen. – BersaKAIN Jun 30 '15 at 19:41
  • Is there a reason you have different heights? – Aggressor Jun 30 '15 at 21:42
  • Your last height is HUGE, no wonder its appearing down. Your cells should have the same height – Aggressor Jun 30 '15 at 21:42
  • The second item (72.895) is the height while the first is the width. So I think all height are actually the same. Thanks for replying! – BersaKAIN Jul 01 '15 at 18:50
  • Sorry I was reading it wrong the width values are different yes – Aggressor Jul 01 '15 at 19:21
  • Possible duplicate of [UICollectionView estimatedItemSize - last cell is not aligned](http://stackoverflow.com/questions/26566911/uicollectionview-estimateditemsize-last-cell-is-not-aligned) – Maksym Gontar Nov 11 '15 at 14:11
  • possible duplicate http://stackoverflow.com/questions/26566911/uicollectionview-estimateditemsize-last-cell-is-not-aligned – Maksym Gontar Nov 11 '15 at 14:12

2 Answers2

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];
Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
-1

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.

Jorge Sánchez
  • 391
  • 4
  • 7