ios - how to specify height of row? -
i want implement tableview shows expandable cell specific row, create custom table cell expanded if setting expandcontent following:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; customcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; nsstring *shouldexpand = [self.datasource objectatindex:indexpath.row]; if([shouldexpand isequaltostring:@"expand"]){ [cell setexpandcontent:@"expand"]; } else{ [cell settitle:@"a line"]; } return cell; }
however, in order tell tableview row height, need implement following code:
- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { customcell *cell = [self tableview:tableview cellforrowatindexpath:indexpath]; return [cell cellheight]; }
the problem heightforrowatindexpath method call 1000 times of tableview:cellforrowatindexpath: if datasource contains 1000 data, , cost time.
how fix problem?
no u should find size of cell first send hight calculated, dont call tableview:cellforrowatindexpath: results in recursion instead, first calculate , send height . example
//say suppose placing string inside tableview cell u need calculate cell example nsstring *string = @"hello world happy coding"; cgsize maxsize = cgsizemake(280, maxfloat);//set max height cgsize cellsize = [self.str sizewithfont:[uifont systemfontofsize:17] constrainedtosize:maxsize linebreakmode:nslinebreakbywordwrapping];//this return correct height text return cellsize.height +10; //finally u return height
Comments
Post a Comment