Search code examples
iosswiftuicollectionviewuiimageviewuicollectionviewcell

Swift - pick image and label for UICollectionViewCell


right now I have a collectionView in which I can add cells by tapping the "addCell".

enter image description here enter image description here

My goal is that if the user taps the "addCell" a view should appear where the user can type in a title for the cell and select an image like so:

enter image description here

Any idea on how I could realize that?

class ContentCell: UICollectionViewCell {
let testImage: UIImageView = {
    let v = UIImageView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .cyan
    return v
}()

let testLabel: UILabel = {
    let v = UILabel()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.text = "Test Label"
    v.font = UIFont(name: "Avenir Next-Bold", size: 18)
    v.textColor = .darkGray
    v.textAlignment = .center
    return v
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    commonInit()
}

func commonInit() -> Void {

    contentView.addSubview(testLabel)
    contentView.addSubview(testImage)

    NSLayoutConstraint.activate([

        testImage.topAnchor.constraint(equalTo: contentView.topAnchor),
        testImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        testImage.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
        testImage.heightAnchor.constraint(equalToConstant:150),

        testLabel.topAnchor.constraint(equalTo: testImage.bottomAnchor,constant: 1),
        testLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        testLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        testLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
    ])
}

}


Solution

  • so in didSelectItemAtIndexPath you present the view controller to ask for the image image and title, and then in that view controller you declare a delegate or completion handler that when the user presses "done" or "save" in the view controller where they selected the image and title, the delegate or completion handler is then called and then tells the original view controller with the cells to add the item you just create and reload the collectionView. this is pretty standard stuff and this same pattern you'll need to learn how to use since most apps use this this same pattern 100s of times per app. i can show you from Objective-C but won't waste my time showing this is Swift. lmk if you need more help. good luck

    oh yes, and for future reference, it's best to stick with delegation since using completion handlers gets very tricky when you encounter cases with nested completions and still need to maintain a weakified then strongified reference to the controller. after two degress of weakifying and strogifying "self" the system will start to randomly deallocate your controllers. Stick with delegation.