swift3 - How to count number of uicollectionview's rows in swift -
i need determine uicollectionview's height.
let layout = contactcollectionview.collectionviewlayout let heightsize = string(int(layout.collectionviewcontentsize.height))
the above solution works on situations in project. in specific situation need count number of rows , multiple number find height.
func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int { // height = (count rows) * 30 . every cell height 30 // every cell has different width. return (presenter?.selectedcontact.count)! }
how can find number of rows?
update
this collectionview. every cell has different width(because string). has different rows. width of collectionview width of view.frame
you can compare if collectionview
's width
greater total previous width(s) increment rowcounts one. assuming implementing uicollectionviewdelegateflowlayout
methods , per each cell know dynamic width
@ moment. if won't know width @ moment, can calculate width
of string
take considering uifont
along other stuff in each cell.
here reference how calculate width
of string
https://stackoverflow.com/a/30450559/6106583
something below
//stored properties var totalwidthperrow = cgfloat(0) var rowcounts = 0 let spacebetweencell = cgfloat(10) // whatever space between each cell func collectionview(_collectionview:uicollectionview,layoutcollectionviewlayout:uicollectionviewlayout,sizeforitemat indexpath: indexpath) -> cgsize { let collectionviewwidth = view.frame.width let dynamiccellwidth = //whatever calculate width totalwidthperrow += dynamiccellwidth + spacebetweencell if (totalwidthperrow > collectionviewwidth) { rowcounts += 1 totalwidthperrow = dynamiccellwidth + spacebetweencell } return cgsizemake(dynamiccellwidth , cgfloat(30)) }
Comments
Post a Comment