objective c - Calculating dynamically the height of a custom UITableViewCell -
i building application without ib , writing scratch. have custom uitableviewcell , when try calculate height, behaves there no subviews. find lot of discussion dynamically setting height, nothing how calculate it, based on subviews , how , set constraints. did setting constraints first component - uiimageview , add them contenview of cell. 0.
i appreciate input on or @ least direction!
heightforrowatindexpath:
-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ uitableviewcell* cell = [[uitableviewcell alloc]init]; cell.imageview.image = [uiimage imagenamed:@"avatar"]; cell.imageview.translatesautoresizingmaskintoconstraints=no; nsarray *constraints = [nslayoutconstraint constraintswithvisualformat:@"h:|-30-[image]" options:0 metrics:nil views:@{ @"image": cell.imageview }]; [cell.contentview addconstraints:constraints]; nsarray *constraints2 = [nslayoutconstraint constraintswithvisualformat:@"v:|-20-[image]" options:0 metrics:nil views:@{ @"image": cell.imageview}]; [cell.contentview addconstraints:constraints2]; [ nslayoutconstraint constraintwithitem:cell.imageview attribute: nslayoutattributewidth relatedby:nslayoutrelationequal toitem:cell.imageview attribute:nslayoutattributeheight multiplier:1.0 constant:0.0f]; cgfloat height = [cell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize].height; nslog(@"%f",height); return height; }
my solution kind of problem uitableviewautomaticdimension
. return in heightforrowatindexpath
method. have set tableview.estimatedrowheight
value close get.
for instance, if have 5 cells height [100,200,250,270,300] can set estimatedrowheight
220 (or similar).
you can take @ first answer @ using auto layout in uitableview dynamic cell layouts & variable row heights well.
Comments
Post a Comment