ios - Error in sizing UICollectionViewCell in Swift -
i trying adjust size of each collection view cell according length of label text contained within
func collectionview(collectionview: uicollectionview, layout collectionviewlayout:uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { var size = cgsize() var cell = collectionview.dequeuereusablecellwithreuseidentifier("lessoncell", forindexpath: indexpath) uicollectionviewcell var label: uilabel = cell.viewwithtag(300) uilabel var labelsize = label.frame.size size = labelsize homecoming size } when running code, app crashes error 'negative or 0 sizes not supported in flow layout.' however, when stepped through, found crash occurs in initializing cell variable, before size determined. why initializing cell variable throw type of error?
i found problem. using collectionview.dequeuereusablecellwithreuseidentifier() when in reality should used "cellforitematindexpath" delegate method. worked me next code:
func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { var size = cgsize(width: 0, height: 0) var label = uilabel() label.text = category[indexpath.row] label.sizetofit() var width = label.frame.width size = cgsize(width: (width+20), height: 50) homecoming size } ios swift uicollectionview
No comments:
Post a Comment