简体   繁体   中英

Add Subview within a custom subclassed UICollectionViewCell class

This might be a simple question but I can't get it work nor can I find an answer to the question.

I am trying to add a subview to a custom subclassed collectionviewcell when calling a method from the viewcontroller.

I have a didSelectItemAtIndexPath created in the viewcontroller. When I click one of the cells, I call the addOverlay method in the custom cell class to add a subview but this does not seem to show up. Any help would be useful. Thank you

Sample code:

func addOverlayView() { let overlayView = UIView(frame: CGRectMake(0, 0, 50, 50)) overlayView.backgroundColor = UIColor.blueColor() contentView.addSubview(overlayView) }

Did you get a proper cell reference like this at the didSelectItemAtIndexPath method?

if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? YourCustomCollectionViewCell {
   cell.addOverlayView()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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