ios - UITableViewController scrolling optimisations for "Messenger App"-like functions -


i developing small app, includes messenger functionality. when showing chat thread used tableview, populate messages in small bubbles , @ point before viewdidappear scroll bottom, recent messages shown.

there popular solution scrolling bottom of tableview scrolltocellatindexpath or scrollrecttovisible. these kinda work little data, , constant cell heights. in case there lot of message data , use uitableviewautomaticdimensions rowheight. results in slow loading, not scrolling bottom , crazy error messages.

here's why (imo): scroll bottom, tableview has load cells because have automatic dimensions, , can scroll down after knows how far is. auto layout problem. doesn't scroll way down, because auto layout didn't finish yet , rowheight @ initial value still.

what tried: putting scrolling in didlayoutsubviews: solves problem loads slowly, , scrolling called multiple times (+ crazy error message)

i guess upside down tableview solve problem, because first cell latest message, , no scrolling needed.

for optimization, should use following method:

func tableview(tableview: uitableview, estimatedheightforrowatindexpath indexpath: nsindexpath) -> cgfloat 

it designed large data sets, because provides estimates calculations instead of pixel-perfect size of cells (you have provide normal method getting height - obviously).

second optimization can when have cells not change height time (that is, have same height every time), store calculated height each nsindexpath , use value instead of calculating again. height of cell autolayout, can use this:

    // configure     self.configurecell(cell!, indexpath: path)      // update constraints     cell!.setneedsupdateconstraints()     cell!.updateconstraintsifneeded()     cell!.contentview.setneedslayout()     cell!.contentview.layoutifneeded()      // calculate height of cell     let height : cgfloat = cell!.contentview.systemlayoutsizefittingsize(uilayoutfittingcompressedsize).height 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -