ios - Setting height for UITableViewCell with UIWebView inside -
i have uiwebview
inside custom uitableviewcells
. problem don't quite know how dynamically size uitableviewcell
cells since webview inside cell needs render before can know webview's height.
my proposed solution follows:
customcell.m conforms uiwebviewdelegate
, returns own height delegate method after webview inside has finished loading.
- (void)webviewdidfinishload:(uiwebview *)webview { cgfloat height = [[webview stringbyevaluatingjavascriptfromstring:@"document.body.scrollheight"] floatvalue]; [self.contentwebview setframeheight:height]; [self.delegate celldidfinishloadingwithcell:self withheight:height]; }
then tableviewcontroller.m conforms cell's delegate, , redraws doing like:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nonnull nsindexpath *)indexpath { return [self.heights[[nsstring stringwithformat:@"%i%i",indexpath.section,indexpath.row]] floatvalue]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uiwebviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"webview-cell" forindexpath:indexpath]; mydata *d = self.data[indexpath.section]; [cell setupcellfordata:d]; [cell setdelegate:self]; return cell; } - (void)celldidfinishloadingwithcell:(lessontableviewcell *)cell withheight:(cgfloat)height { nsindexpath *indexpath = [self.tableview indexpathforcell:cell]; nsstring *key = [nsstring stringwithformat:@"%i%i",indexpath.section,indexpath.row]; if (!self.heights[key]) { [self.heights setobject:[nsnumber numberwithfloat:height] forkey:key]; } [self.tableview beginupdates]; [self.tableview endupdates]; }
some cells seem correct height, many of them end having height of 0...
remove :
[self.tableview beginupdates]; [self.tableview endupdates];
add :
[self.tableview reloaddata];
apple documents:
beginupdates
reloaddata
Comments
Post a Comment