I am following an old tutorial about how to make a tag flow system on collection view. I have managed to display the full labels with different sizes each, but I am struggling to make the cells resize themselves. I am a beginner in Swift/Xcode so the error must be silly, but I can't figure it out.

I have followed this tutorial but mine doesn't work. I have changed a little bit according to what Xcode recommended because the syntax there is a bit old. I think I might have misunderstood something.

I have uploaded the project to GitHub, but what I think is the issue is the following:

This global variable is supposed to be a cell for an overwritten function to check the size:

var sizingCell: TagCell?

On the viewDidLoad() I grab the cell with

self.sizingCell = (cellNib.instantiate(withOwner: nil, options: nil) as NSArray).firstObject as! TagCell

And on the extension

extension ViewController: UICollectionViewDataSource{
...
}

I have the following functions to adjust the size of the cell (from the tutorial)

func configureCell(cell: TagCell, forIndexPath indexPath: IndexPath){
    let tag = TAGS[indexPath.row]
    cell.tagName.text = tag
}

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize{
    self.configureCell(cell: self.sizingCell!, forIndexPath: indexPath)
    return self.sizingCell!.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
}

I expected for different rows to be able to have different amount of tags (like in a flow layout) and each tag cell with a different width. Instead, every cell has the size size, but the tags are allowed to have different sizes.

0 Answers