ios - UICollectionView cells do not take up width of UICollectionView -
the uicollectionview width change per device constrained reach horizontal margins of view. collectionview's layout flow. reason using uicollectionview space images evenly across on different device sizes, , of these images have tap gesture recogniser. , amount of rows variable, amount of image icons vary. apparently method after:
func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { return cgsize(width: collectionviewwidth/6, height: collectionviewwidth/6); }
i have tried this:
var collectionviewwidth: cgfloat = 0.0 override func viewdidload() { super.viewdidload() collectionview.datasource = self } override func viewdidappear(animated: bool) { collectionviewwidth = collectionview.collectionviewlayout .collectionviewcontentsize().width } func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { return cgsize(width: collectionviewwidth/6, height: collectionviewwidth/6); }
but nothing try in code makes difference @ final output (forget images not in uicollectionview - deleted):
i changed size of cells in interface builder , result got me this:
so have 2 questions:
why cells in above picture not go edge of uicollectionview?
how can set cell size programmatically @ run time make uicollectionview have 6 columns on device sizes?
here code viewcontroller contains uicollectionview:
import uikit class dressingroomviewcontroller: uiviewcontroller { @iboutlet weak var collectionview: uicollectionview! let identifier = "cellidentifier" let datasource = datasource() var collectionviewwidth: cgfloat = 0.0 override func viewdidload() { super.viewdidload() collectionview.datasource = self } override func viewdidappear(animated: bool) { collectionviewwidth = collectionview.collectionviewlayout .collectionviewcontentsize().width } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } override func prepareforsegue( segue: uistoryboardsegue, sender: anyobject?) { if (segue.identifier == "dressingroom2myoutfits") { let myoutfitsviewcontroller = segue.destinationviewcontroller as! myoutfitsviewcontroller } } } // mark:- uicollectionviewdatasource delegate extension dressingroomviewcontroller : uicollectionviewdatasource { func numberofsectionsincollectionview( collectionview: uicollectionview) -> int { return datasource.groups.count } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return datasource.numbeofrowsineachgroup(section) } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier( identifier,forindexpath:indexpath) as! fruitcell let fruits: [fruit] = datasource.fruitsingroup(indexpath.section) let fruit = fruits[indexpath.row] let name = fruit.name! cell.imageview.image = uiimage(named: name) cell.caption.text = name.capitalizedstring return cell } func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { return cgsize(width: collectionviewwidth/6, height: collectionviewwidth/6); } }
it may worth mentioning data source of uicollectionview grouped because doing tutorial. here datasource code:
class datasource {
init() { populatedata() } var fruits:[fruit] = [] var groups:[string] = [] func numbeofrowsineachgroup(index: int) -> int { return fruitsingroup(index).count } func numberofgroups() -> int { return groups.count } func gettgrouplabelatindex(index: int) -> string { return groups[index] } // mark:- populate data plist func populatedata() { if let path = nsbundle.mainbundle().pathforresource( "fruits", oftype: "plist") { if let dictarray = nsarray(contentsoffile: path) { item in dictarray { if let dict = item as? nsdictionary { let name = dict["name"] as! string let group = dict["group"] as! string let fruit = fruit(name: name, group: group) if !contains(groups, group){ groups.append(group) } fruits.append(fruit) } } } } } // mark:- fruitsforeachgroup func fruitsingroup(index: int) -> [fruit] { let item = groups[index] let filteredfruits = fruits.filter { (fruit: fruit) -> bool in return fruit.group == item } return filteredfruits }
}
and here plist gets loaded data source:
edit: have done work remove groups unneeded. see groups causing new rows start soon. changed code this:
// mark:- uicollectionviewdatasource delegate extension dressingroomviewcontroller : uicollectionviewdatasource { func numberofsectionsincollectionview( collectionview: uicollectionview) -> int { return 1 } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 12 } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier( identifier,forindexpath:indexpath) as! fruitcell let fruits: [fruit] = datasource.fruits let fruit = fruits[indexpath.row] let name = fruit.name! cell.imageview.image = uiimage(named: name) cell.caption.text = name.capitalizedstring return cell } func collectionview(collectionview: uicollectionview, sizeforitematindexpath indexpath: nsindexpath) -> cgsize { return cgsize(width: 200, height: 200); } }
now need make cell width 1/6 of collection view width somehow , crop off bottom of uicollectioview wasted space
you can set itemsize of collectionviewlayout in viewdidload shown in example:
override func viewdidload() { super.viewdidload() let layout = collectionview.collectionviewlayout as! uicollectionviewflowlayout let collectionviewsize = collectionview.frame.size let nrofcellsperrow = 6 let itemwidth = collectionviewsize.width/nrofcellsperrow let itemheight = 80 // example layout.itemsize = cgsize(width: itemwidth, height: itemheight) }
if want have spacing between cells have compensate itemwidth calculation
Comments
Post a Comment