ios - how to set up something like a collectionview in a tableview? -
here visual example of achieve.
my initial idea use uicollectionview custom cells, didn't work out , trying out collectionview within collectionview worse.
so tried collectionview in tableview , kind of worked tableviewcell's dynamic height proved gigantic issue super difficult me solve have begun learn swift , ios developing. please :(
edit: note "shopping mall 1" section meant sticky header of sort. similar (the turquoise header):
credits: https://github.com/jamztang/csstickyheaderflowlayout
but using https://github.com/petec-blog/collectionviewstickyheaders example clearer , closer needed.
you play around code below idea (xcode 7.2 (7c68)) delete storyboard, launchscreen, clean properties main story board , launchscreen in info.plist , replace appdelegate.swift following content
import uikit @uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate { var window: uiwindow? func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { self.window = uiwindow(frame: uiscreen.mainscreen().bounds) self.window!.rootviewcontroller = collectionviewintableview() self.window!.makekeyandvisible() return true } } class collectionviewintableview: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { let table = uitableview() override func viewdidload() { table.delegate = self table.datasource = self self.view.backgroundcolor = uicolor.whitecolor() table.registerclass(cell.self, forcellreuseidentifier: cell.self.description()) self.view.addsubviewwithconstraints(["v" : table], constraints: ["h:|[v]|", "v:|-50-[v]|"]) } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {return 2} func numberofsectionsintableview(tableview: uitableview) -> int {return 2} func tableview(tableview: uitableview, titleforheaderinsection section: int) -> string? {return "shopping moll \(section)"} func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { return tableview.dequeuereusablecellwithidentifier(cell.self.description(), forindexpath: indexpath) as! cell } func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat {return 200} } class cell: uitableviewcell, uicollectionviewdatasource, uicollectionviewdelegateflowlayout { var collectionview: uicollectionview! override init(style: uitableviewcellstyle, reuseidentifier: string?) { super.init(style: style, reuseidentifier: reuseidentifier) self.selectionstyle = uitableviewcellselectionstyle.none let layout = uicollectionviewflowlayout() layout.sectioninset = uiedgeinsets(top: 20, left: 10, bottom: 10, right: 10) layout.itemsize = cgsize(width: 75, height: 60) layout.headerreferencesize = cgsize(width: contentview.frame.width, height: 20) collectionview = uicollectionview(frame: cgrect(x: 0, y: 0, width: uiscreen.mainscreen().bounds.width, height: 200), collectionviewlayout: layout) collectionview.datasource = self collectionview.delegate = self collectionview.registerclass(item.self, forcellwithreuseidentifier: item.self.description()) collectionview.registerclass(sectiontitle.self, forsupplementaryviewofkind: uicollectionelementkindsectionheader, withreuseidentifier: sectiontitle.self.description()) self.backgroundcolor = uicolor.yellowcolor() collectionview.backgroundcolor = uicolor.whitecolor() self.contentview.addsubview(collectionview) } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {return 10} func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { return collectionview.dequeuereusablecellwithreuseidentifier(item.self.description(), forindexpath: indexpath) as! item } required init?(coder: nscoder) {super.init(coder: coder)} func collectionview(collectionview: uicollectionview, viewforsupplementaryelementofkind kind: string, atindexpath indexpath: nsindexpath) -> uicollectionreusableview { switch kind { case uicollectionelementkindsectionheader: let headerview = collectionview.dequeuereusablesupplementaryviewofkind(kind, withreuseidentifier: sectiontitle.self.description(), forindexpath: indexpath) as! sectiontitle headerview.label.text = "shop \(indexpath.section)" return headerview default: assert(false, "unexpected element kind") } } } class sectiontitle: uicollectionreusableview { var label: uilabel! override init(frame: cgrect) { super.init(frame:cgrectzero) label = uilabel() label.text = "text" self.addsubviewwithconstraints(["v" : label], constraints: ["h:|-10-[v]|","v:|[v]|"]) } required init?(coder adecoder: nscoder) {fatalerror("init(coder:) has not been implemented")} } class item: uicollectionviewcell { override init(frame: cgrect) { super.init(frame: frame) let v = uiview(frame: cgrect(x: 0, y: 0, width: 50, height: 50)) v.backgroundcolor = uicolor.bluecolor() contentview.addsubview(v) } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } } extension uiview { func addsubviewwithconstraints(views: [string : anyobject], constraints: array<string>) -> [string : anyobject] { (_, view) in views { self.addsubview(view as! uiview) (view as! uiview).translatesautoresizingmaskintoconstraints = false } var = 0; < constraints.count; i++ {self.addconstraints(nslayoutconstraint.constraintswithvisualformat(constraints[i], options: nslayoutformatoptions(rawvalue: 0), metrics: nil, views: views))} return views } }
Comments
Post a Comment