ios - Self sizing cell just doesn't work for me -
i tried follow wwcd 2014 session 226, introduced way realize self sizing cells in ios 8 using auto layout, , doesn't work should.
hhtableviewcell.h
#import <uikit/uikit.h> @interface hhtableviewcell : uitableviewcell @property (strong, nonatomic)uilabel *title; @end
hhtableviewcell.m
#import "hhtableviewcell.h" @implementation hhtableviewcell @synthesize title = _title; - (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier { self = [super initwithstyle:style reuseidentifier:reuseidentifier]; if (self) { // configure control(s) #pragma mark -- title lable _title = [[uilabel alloc] initwithframe:cgrectinset(self.bounds, 15.0, 0.0)]; _title.font = [uifont preferredfontfortextstyle:uifonttextstyleheadline]; _title.numberoflines = 0; [self.contentview addsubview:_title]; #pragma mark -- constraints nsmutablearray *constraints = [[nsmutablearray alloc]init]; uiview *contentview = self.contentview; [constraints addobject:[nslayoutconstraint constraintwithitem:_title attribute:nslayoutattributefirstbaseline relatedby:nslayoutrelationequal toitem:contentview attribute:nslayoutattributetop multiplier:1.8 constant:3.0]]; [constraints addobject:[nslayoutconstraint constraintwithitem:_title attribute:nslayoutattributefirstbaseline relatedby:nslayoutrelationequal toitem:contentview attribute:nslayoutattributetop multiplier:1.8 constant:3.0]]; [constraints addobject:[nslayoutconstraint constraintwithitem:_title attribute:nslayoutattributefirstbaseline relatedby:nslayoutrelationequal toitem:contentview attribute:nslayoutattributetop multiplier:1.8 constant:3.0]]; [constraints addobject:[nslayoutconstraint constraintwithitem:contentview attribute:nslayoutattributeheight relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:0 multiplier:1.0 constant:44.0]]; [constraints addobjectsfromarray:[nslayoutconstraint constraintswithvisualformat:@"h:|-15-[_title]-15-|" options:0 metrics:nil views:nsdictionaryofvariablebindings(_title)]]; [self.contentview addconstraints:constraints]; } return self; } @end
mmtableviewcontroller.h
#import <uikit/uikit.h> @interface mmtableviewcontroller : uitableviewcontroller @end
mmtableviewcontroller.m
#import "mmtableviewcontroller.h" #import "hhtableviewcell.h" @interface mmtableviewcontroller () @end @implementation mmtableviewcontroller - (void)viewdidload { [super viewdidload]; [self.tableview registerclass:[hhtableviewcell class] forcellreuseidentifier:@"hicell"]; [self.tableview registerclass:[hhtableviewcell class] forcellreuseidentifier:@"hcell"]; self.tableview.estimatedrowheight = 44.0; self.tableview.rowheight = uitableviewautomaticdimension; // uncomment following line preserve selection between presentations. // self.clearsselectiononviewwillappear = no; // uncomment following line display edit button in navigation bar view controller. // self.navigationitem.rightbarbuttonitem = self.editbuttonitem; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { #warning potentially incomplete method implementation. // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { #warning incomplete method implementation. // return number of rows in section. return 10; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"hicell"; hhtableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; cell.title.text = @"hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. hello apple. "; // configure cell... nslog(cell.title.text); return cell; } @end
cell of fixed height , wrapping 2 lines of text. looks this:
hello apple. hello apple. hello apple. hello
aplle. hello apple. hello apple. hello apple...
constraints , subviews added programatically. simulator running ios 8.3 in xcode 6.3.1.
for uilabel work constraints, looking @ apple's documentation, think need set preferredmaxlayoutwidth
property:
this property affects size of label when layout constraints applied it. during layout, if text extends beyond width specified property, additional text flowed 1 or more new lines, thereby increasing height of label.
however, unless want specific cell customization, can use default uitableviewcell, , set numberoflines = 0
on provided titlelabel
. work uitableviewautomaticdimension
, although i've tested in conjunction heightforrowatindexpath:
.
update:
from i've learned, need set estimatedrowheight
in viewdidload
(the value doesn't needs accurate/important seems).
here working example using default uitableviewcells:
Comments
Post a Comment