prevent the uicollectionview cell form moving to the right when there is space -- SWIFT -
my problem uicollectionview cell moving left when next cell jumps second line , there space in first line (space didnt fit contain next cell), how prevent cell moving right, when there still space, need thing setmaximumspace between cells (which doesnt exist) ?
here code :
func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = mycollectionview.dequeuereusablecellwithreuseidentifier("charaktercell", forindexpath: indexpath) charaktercollectionviewcell (var i=0 ; i<4 ; i++) { if (collectioncellindex[i] == nil) { collectioncellindex[i] = indexpath println("\(collectioncellindex[i])") println(i) break } } return cell } func collectionview(collectionview: uicollectionview, layout collectionviewlayout:uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { var size = cgsize(width: 0, height: 0) var label = uilabel() if (selectedcharincollectionnames[indexpath.row] == "") { size = cgsize(width: 40, height: 30) } else { label.text = selectedcharincollectionnames[indexpath.row] label.sizetofit() var width = label.frame.width size = cgsize(width: (width+25), height: 30) } label.sizetofit() return size } private let sectioninsets = uiedgeinsets(top: 10, left: 10, bottom: 10, right: 10) func collectionview(collectionview: uicollectionview!, layout collectionviewlayout: uicollectionviewlayout!, insetforsectionatindex section: int) -> uiedgeinsets { return sectioninsets }
@kevin r answer here! solved question
class alignleftflowlayout: uicollectionviewflowlayout { var maximumcellspacing = cgfloat(9.0) override func layoutattributesforelementsinrect(rect: cgrect) -> [anyobject]? { let attributestoreturn = super.layoutattributesforelementsinrect(rect) as? [uicollectionviewlayoutattributes] attributes in attributestoreturn ?? [] { if attributes.representedelementkind == nil { attributes.frame = self.layoutattributesforitematindexpath(attributes.indexpath).frame } } return attributestoreturn } override func layoutattributesforitematindexpath(indexpath: nsindexpath) -> uicollectionviewlayoutattributes! { let curattributes = super.layoutattributesforitematindexpath(indexpath) let sectioninset = (self.collectionview?.collectionviewlayout uicollectionviewflowlayout).sectioninset if indexpath.item == 0 { let f = curattributes.frame curattributes.frame = cgrectmake(sectioninset.left, f.origin.y, f.size.width, f.size.height) return curattributes } let previndexpath = nsindexpath(foritem: indexpath.item-1, insection: indexpath.section) let prevframe = self.layoutattributesforitematindexpath(previndexpath).frame let prevframerightpoint = prevframe.origin.x + prevframe.size.width + maximumcellspacing let curframe = curattributes.frame let stretchedcurframe = cgrectmake(0, curframe.origin.y, self.collectionview!.frame.size.width, curframe.size.height) if cgrectintersectsrect(prevframe, stretchedcurframe) { curattributes.frame = cgrectmake(prevframerightpoint, curframe.origin.y, curframe.size.width, curframe.size.height) } else { curattributes.frame = cgrectmake(sectioninset.left, curframe.origin.y, curframe.size.width, curframe.size.height) } return curattributes }
}
Comments
Post a Comment