ios - UICollectionView dynamic custom layout -


i'm trying create uicollectionview type of layout image i've attached. , i'm little confused how achieve layout.

after doing googling seems need write custom uicollectionviewflowlayout, can't seem find examples of layouts have different section counts per item.

if @ mockup first item has 1 section. image landscape, middle item has 2 sections 2 portrait images.

am looking wrong thing? point me in right direction?

layout mockup

you can using uicollectionviewdelegateflowlayout, no need subclass.

here example:

import uikit  class viewcontroller: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegate, uicollectionviewdelegateflowlayout {      @iboutlet weak var collectionview: uicollectionview!      override func viewdidload() {         super.viewdidload()         collectionview.delegate = self         collectionview.datasource = self     }      // mark: - uicollectionviewdatasource      func numberofsectionsincollectionview(collectionview: uicollectionview) -> int {         return 1     }      func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         return 10     }      func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {         let cell = collectionview.dequeuereusablecellwithreuseidentifier("testcollectionviewcell", forindexpath: indexpath) as! uicollectionviewcell         if indexpath.item % 3 == 0 {             cell.backgroundcolor = uicolor.redcolor()         } else {             cell.backgroundcolor = uicolor.greencolor()         }         return cell     }      // mark: - uicollectionviewdelegateflowlayout      func collectionview(collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitematindexpath indexpath: nsindexpath) -> cgsize {         let flowlayout = collectionviewlayout as! uicollectionviewflowlayout         if indexpath.item % 3 == 0 {             let cellwidth = (cgrectgetwidth(collectionview.frame) - (flowlayout.sectioninset.left + flowlayout.sectioninset.right))             return cgsize(width: cellwidth, height: cellwidth / 2)         } else {             let cellwidth = (cgrectgetwidth(collectionview.frame) - (flowlayout.sectioninset.left + flowlayout.sectioninset.right) - flowlayout.minimuminteritemspacing) / 2             return cgsize(width: cellwidth, height: cellwidth)         }     }  } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -