ios - Custom table view cell in Swift, without Storyboard -
i'm new learning ios development , swift language, , have been trying figure out how create custom uitableviewcell
without use of storyboards/interface builder. i'd able accomplish in swift code. far, i've been able find ones make use of interface builder.
what able create re-usable cell can instantiated in table view. understand should able create custom cell sub-views, whos data can set table view's incoming data. right?
right have uinavigationcontroller
embedded, sub-classed uitableviewcontroller
. following other tutorials i've learned how create struct , prepare test data table view cell. that's far i've been able get.
// table view controller class inventorylistviewcontroller: maintableviewcontroller { let viewtitle = "inventory" var inventoryitems = [inventoryitem]() override func viewdidload() { super.viewdidload() self.navigationitem.title = viewtitle.uppercasestring self.inventoryitems = [ inventoryitem(name: "white bread", expiry: "2014-09-12"), inventoryitem(name: "mushrooms", expiry: "2014-09-15"), inventoryitem(name: "watermelon", expiry: nil), inventoryitem(name: "leftover thai", expiry: "2014-09-15"), inventoryitem(name: "cheddar cheese", expiry: "2014-09-12"), inventoryitem(name: "chicken breasts", expiry: "2014-09-10"), inventoryitem(name: "paprika", expiry: nil), inventoryitem(name: "sour cream", expiry: nil) ] // right fail without tableview:cellforrowatindexpath self.tableview.reloaddata() } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return self.inventoryitems.count } } // sub-classed uitableviewcontroller class maintableviewcontroller: uitableviewcontroller { override func viewdidload() { super.viewdidload() self.tableview.separatorcolor = uicolor.clearcolor() self.tableview.contentinset = uiedgeinsetszero } } // data struct cells struct inventoryitem { let name: string let expiry: string? }
i understand swift still new , that's why can't find many resources topic, if has pointers appreciative help!
you can use below code custom cells without storyboard.just register class
, use tableviewcontroller
//cell.swift import foundation import uikit class cell : uitableviewcell{ //do whatever want exta customization }
your controller viewdidload
override func viewdidload() { super.viewdidload() // register class cell identifier self.tableview.registerclass(cell.self anyclass, forcellreuseidentifier: "cell") } //cellforrowatindexpath override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell:cell? = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as? cell if cell == nil { cell = cell(style: uitableviewcellstyle.default, reuseidentifier: "cell") } //configure cell custom code cell.textlabel?.text = @"your label text" return cell! }
Comments
Post a Comment