ios - Custom UITableViewCell with nib and Auto Layout -
i’m getting pretty sick of auto layout uitableview now…
apple’s docs , code demos seem work them, when try no luck. i’ve deleted , rebuilt nibs scratch see if i’ve set properties inadvertently… twice.
trouble is, time round i’ve got auto layout cells working, woo? no. resize when scrolled out of viewport , in. (i’m guessing reload of kind).
has come across issue? there’s few questions similar on no fixes, discussions of how must bug.
here code:
called on uitableview’s iboutlet didset method:
private func loadheadlinestableview() { headlinestableview.delegate = self headlinestableview.datasource = self // register classes/nibs headlinestableview.registerclass(headlineherotableviewcell.self, forcellreuseidentifier: cellidentifiers.heroheadline) headlinestableview.registernib(uinib(nibname: "headlineherotableviewcell", bundle: nil), forcellreuseidentifier: cellidentifiers.heroheadline) headlinestableview.registerclass(headlinebigtableviewcell.self, forcellreuseidentifier: cellidentifiers.bigheadline) headlinestableview.registernib(uinib(nibname: "headlinebigtableviewcell", bundle: nil), forcellreuseidentifier: cellidentifiers.bigheadline) headlinestableview.registerclass(headlinemediumtableviewcell.self, forcellreuseidentifier: cellidentifiers.mediumheadline) headlinestableview.registernib(uinib(nibname: "headlinemediumtableviewcell", bundle: nil), forcellreuseidentifier: cellidentifiers.mediumheadline) headlinestableview.registerclass(headlinesmalltableviewcell.self, forcellreuseidentifier: cellidentifiers.smallheadline) headlinestableview.registernib(uinib(nibname: "headlinesmalltableviewcell", bundle: nil), forcellreuseidentifier: cellidentifiers.smallheadline) // automatic cell height headlinestableview.rowheight = uitableviewautomaticdimension headlinestableview.estimatedrowheight = 128 headlinestableview.backgroundview = nil headlinestableview.backgroundcolor = uicolor.clearcolor() } cellforrowatindexpath:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { if tableview.isequal(headlinestableview) { // headlines var cell: headlinetableviewcell! switch indexpath.section { case headlinesections.index.heroheadlines: // hero cell = tableview.dequeuereusablecellwithidentifier(cellidentifiers.heroheadline, forindexpath: indexpath) as? headlineherotableviewcell case headlinesections.index.bigheadlines: // big cell = tableview.dequeuereusablecellwithidentifier(cellidentifiers.bigheadline, forindexpath: indexpath) as? headlinebigtableviewcell case headlinesections.index.mediumheadlines: // medium cell = tableview.dequeuereusablecellwithidentifier(cellidentifiers.mediumheadline, forindexpath: indexpath) as? headlinemediumtableviewcell case headlinesections.index.smallheadlines: // small cell = tableview.dequeuereusablecellwithidentifier(cellidentifiers.smallheadline, forindexpath: indexpath) as? headlinesmalltableviewcell default: break } // return cell return cell } return uitableviewcell() } willdisplaycell:
func tableview(tableview: uitableview, willdisplaycell cell: uitableviewcell, forrowatindexpath indexpath: nsindexpath) { if tableview.isequal(headlinestableview) { // headlines let headlinecell = cell as! headlinetableviewcell let headline = headlineforindexpath(indexpath) headlinecell.configurewithheadline(headline) if let thumbnailurl = headline.thumbnailurl { headlineimageforurl(thumbnailurl, completionhandler: { (thumbnail) -> () in headlinecell.configurewiththumbnail(thumbnail) }) } } } didenddisplayingcell:
func tableview(tableview: uitableview, didenddisplayingcell cell: uitableviewcell, forrowatindexpath indexpath: nsindexpath) { if tableview.isequal(headlinestableview) { // headlines let headline = headlineforindexpath(indexpath) if let cachekey = headline.thumbnailurl?.absolutestring, let downloader = headlineimagesdownloading[cachekey] { downloader.cancelretreivingthumbnailimage() headlineimagesdownloading.removevalueforkey(cachekey) } } } custom cell class:
class headlinetableviewcell: uitableviewcell { // mark: - properties // mark: outlets @iboutlet private weak var headlineimageview: uiimageview! @iboutlet private weak var headlinetitlelabel: uilabel! @iboutlet private weak var headlinedescriptionlabel: uilabel! // mark: - view lifecycle override func awakefromnib() { super.awakefromnib() // initialization code } // mark: - methods func configurewithheadline(headline: headline?) { headlinetitlelabel?.text = headline?.title headlinedescriptionlabel?.text = headline?.paragraph } func configurewiththumbnail(thumbnail: uiimage?) { headlineimageview?.image = thumbnail } } here’s screenshots of custom cell nib. more advanced i’m struggling label…
i know it’s lot have no idea going wrong here… thanks
okay, after ton of work , having buy applecare before “accidentally” dropping mac against wall, seems calls tableview(_:willdisplaycell:forrowatindexpath:) whereby might affect contents of cell, i.e cell configuration methods, disrupts auto layout.
i’m sure read somewhere tableview(_:willdisplaycell:forrowatindexpath:) supposed used cell configuration closer @ apple docs says:
a table view sends message delegate before uses cell draw row, thereby permitting delegate customize cell object before displayed. method gives delegate chance override state-based properties set earlier table view, such selection , background color. after delegate returns, table view sets alpha , frame properties, , when animating rows slide in or out.
“customise cell object before displayed”, not contents.
annoyingly there’s little confusion tableview(_:cellforrowatindexpath:) doesn’t should customise contents in method:
the returned uitableviewcell object 1 application reuses performance reasons. should fetch created cell object marked reuse sending dequeuereusablecellwithidentifier: message tableview. various attributes of table cell set automatically based on whether cell separator , on information data source provides, such accessory views , editing controls.
in words of dumbledore, "well fun."



Comments
Post a Comment