ios - Calling cellForRowAtIndexPath from within heightForRowAtIndexPath — alternate? -
i'm trying call cellforrowatindexpath
within heightforrowatindexpath
in order assign height based on cell's type (i'm subclassing uitableviewcell
). trivial, right? well, calling there causes loop. can't quite seem figure out why be. placing breakpoints in both methods doesn't yield anything—the delegate method cellforrowatindexpath
never gets called. take look:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { switch indexpath.row { case 0: return subclasscelltypeone() default: return subclasscelltypetwo() } } func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { // calling cellforrowatindexpath here causes loop let cell = tableview.cellforrowatindexpath(indexpath)! if cell subclasscelltypeone { return uitableviewautomaticdimension } else { return 100 } }
any idea why that's happening? , suggestions on how around it? thanks!
when reference cell made via uitableview
, (usually ios, when loading view), ios calls methods in lifecycle - e.g., heightforrowatindexpath
, editingstyleforrowatindexpath
work out how display etc.
so source of infinite loop make reference cell, inside method called when reference cell made ;)
to fix this, should reference data source, instead of asking cell directly itself. if have class set data collection, easy.
Comments
Post a Comment