ios - UITableViewCell height incorrect, sizeToFit sizes incorrectly -
i attempting create custom uitableviewcell, , having issues cell frame having proper height. troubling because cell sizes correctly iphones 4s/5s running ios 8.4, not iphones 6/6+ running same os.
chaos ensues around calling sizetofit
on messagelabel
. of labels appear have extra, blank lines below, not tall cell makes them out be.
below custom cell. label appears cause trouble messagelabel. view frames of labels, let borders = true
// // notestableviewcell.swift // urchin // // created ethan on 6/17/15. // copyright (c) 2015 tidepool. rights reserved. // import foundation import uikit let notecellheight: cgfloat = 128 let notecellinset: cgfloat = 16 let labelspacing: cgfloat = 6 class notecell: uitableviewcell { let borders = false var cellheight: cgfloat = cgfloat() let usernamelabel: uilabel = uilabel() let timedatelabel: uilabel = uilabel() var messagelabel: uilabel = uilabel() func configurewithnote(note: note) { usernamelabel.text = note.user!.fullname usernamelabel.font = uifont(name: "opensans-bold", size: 17.5)! usernamelabel.textcolor = uicolor.blackcolor() usernamelabel.sizetofit() let usernamex = notecellinset let usernamey = notecellinset usernamelabel.frame.origin = cgpoint(x: usernamex, y: usernamey) let dateformatter = nsdateformatter() dateformatter.dateformat = "eeee m.d.yy h:mm a" var datestring = dateformatter.stringfromdate(note.timestamp) datestring = datestring.stringbyreplacingoccurrencesofstring("pm", withstring: "pm", options: nsstringcompareoptions.literalsearch, range: nil) datestring = datestring.stringbyreplacingoccurrencesofstring("am", withstring: "am", options: nsstringcompareoptions.literalsearch, range: nil) timedatelabel.text = datestring timedatelabel.font = uifont(name: "opensans", size: 12.5)! timedatelabel.textcolor = uicolor.blackcolor() timedatelabel.sizetofit() let timedatex = contentview.frame.width - (notecellinset + timedatelabel.frame.width) let timedatey = usernamelabel.frame.midy - timedatelabel.frame.height / 2 timedatelabel.frame.origin = cgpoint(x: timedatex, y: timedatey) messagelabel.frame.size = cgsize(width: contentview.frame.width - 2 * notecellinset, height: cgfloat.max) let hashtagbolder = hashtagbolder() let attributedtext = hashtagbolder.boldhashtags(note.messagetext) messagelabel.attributedtext = attributedtext messagelabel.adjustsfontsizetofitwidth = false messagelabel.linebreakmode = nslinebreakmode.bywordwrapping messagelabel.numberoflines = 0 messagelabel.sizetofit() let messagex = notecellinset let messagey = usernamelabel.frame.maxy + 2 * labelspacing messagelabel.frame.origin = cgpoint(x: messagex, y: messagey) contentview.addsubview(usernamelabel) contentview.addsubview(timedatelabel) contentview.addsubview(messagelabel) cellheight = notecellinset + usernamelabel.frame.height + 2 * labelspacing + messagelabel.frame.height + notecellinset if (borders) { usernamelabel.layer.borderwidth = 1 usernamelabel.layer.bordercolor = uicolor.redcolor().cgcolor timedatelabel.layer.borderwidth = 1 timedatelabel.layer.bordercolor = uicolor.redcolor().cgcolor messagelabel.layer.borderwidth = 1 messagelabel.layer.bordercolor = uicolor.redcolor().cgcolor self.contentview.layer.borderwidth = 1 self.contentview.layer.bordercolor = uicolor.bluecolor().cgcolor } self.contentview.frame.size = cgsize(width: self.contentview.frame.width, height: cellheight) } }
and heightforrowatindexpath
:
func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { let cell = notecell(style: .default, reuseidentifier: nil) cell.configurewithnote(notes[indexpath.row]) return cell.cellheight }
the project open source , on github, feel free clone repository , check out of code yourself.
thank you!
unfortunately, can't way because tableview(_:heightforrowatindexpath)
called first , value return used create cell dequeue in tableview(_:cellforrowatindexpath)
. cell can't set own size because time (e.g. awakefromnib
or prepareforresuse
), table view have height value it. there whacky workarounds i've used, it's easier use self-sizing table view cells.
Comments
Post a Comment