uitableview - Dynamic Cell Height autolayout IOS -
i have been following tutorial implement dynamic cell height. dynamic-table-view-cell-height works in ios 7 me , not in ios 8. work in ios 8 on ipad i'm little stumped why doesn't work on iphone. whilst have followed code pretty there couple of differences between tutorial , way have implemented , don't know enough autolayout , tables i'm not sure if causing problem.
firstly on tutorial tableview has trailing , leading edges equal 0 superview. in implementation have created proportional constraints scale tableview within multiple different layouts. explain further:
here picture of tableview using proportional constraints across multiple different devices.
to achieve implemented following constraints:
is possible cell height cannot calculated autolayout because tableview has dynamic height?
the second difference can see between implementation , tutorial data source. taking data call slq3lite database whereas tutorial taking data xml feed. confident data populating cells because when @ implementation on ipad can see data so:
but on iphone appears: table visible no cells have been written table.
when use debugger can see records being retrieved database, not being written table.
here code long (sorry)
#import "favouritedviewcontroller.h" #import "dbmanager.h" #import "favouritedcell.h" @interface favouritedviewcontroller () @property (strong, nonatomic) iboutlet uitableview *tableview; @property (strong, nonatomic) nsarray *favourites; @property (nonatomic, strong) dbmanager *dbmanager; typedef void (^completionblock)(); -(void)loaddata; -(void)reloaddatawithcompletions:(completionblock)completionblock; @end @implementation favouritedviewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view. self.dbmanager = [[dbmanager alloc] initwithdatabasefilename:@"tomhaisdb.sql"]; self.tableview.delegate = self; self.tableview.datasource = self; [self loaddata]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(void)loaddata { nsstring *query = @"select * favourites"; if (self.favourites != nil) { self.favourites = nil; } self.favourites = [[nsarray alloc] initwitharray:[self.dbmanager loaddatafromdb:query]]; /* [self reloaddatawithcompletions:^{ self.tableview.backgroundcolor = [uicolor colorwithred:28.0f/255.0f green:30.0f/255.0f blue:35.0f/255.0f alpha:1]; }];*/ [self reloadtableviewcontent]; } /*-(nsinteger)numberofsectionsintableview:(uitableview *)tableview{ return 1; }*/ -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ return self.favourites.count; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ return [self basiccellatindexpath:indexpath]; } - (void)reloadtableviewcontent { dispatch_async(dispatch_get_main_queue(), ^{ [self.tableview reloaddata]; [self.tableview scrollrecttovisible:cgrectmake(0, 0, 1, 1) animated:no]; }); } -(favouritedcell *)basiccellatindexpath:(nsindexpath *)indexpath { favouritedcell *cell = [self.tableview dequeuereusablecellwithidentifier:@"favouritecell" forindexpath:indexpath]; [self configurebasiccell:cell atindexpath:indexpath]; return cell; } -(void)configurebasiccell:(favouritedcell *)cell atindexpath:(nsindexpath *)indexpath{ nsinteger indexoftomhaistext = [self.dbmanager.arrcolumnnames indexofobject:@"tomhaistext"]; nsstring *tomhaistext = [[self.favourites objectatindex:indexpath.row] objectatindex:indexoftomhaistext]; [self settomhaisforcell:cell item:tomhaistext]; [self setanswerforcell:cell item:tomhaistext]; // change later } -(void)settomhaisforcell:(favouritedcell *)cell item:(nsstring *)item{ [cell.favouritetext settext:item]; } -(void)setanswerforcell:(favouritedcell *)cell item:(nsstring *)item{ [cell.answer settext:item]; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ return [self heightforfavouritecellatindexpath:indexpath]; } -(cgfloat)heightforfavouritecellatindexpath:(nsindexpath *)indexpath{ static favouritedcell *sizingcell = nil; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ sizingcell = [self.tableview dequeuereusablecellwithidentifier:@"favouritecell"]; }); [self configurebasiccell:sizingcell atindexpath:indexpath]; return [self calculateheightforconfiguredsizingcell:sizingcell]; } -(cgfloat)calculateheightforconfiguredsizingcell:(uitableviewcell *)sizingcell{ sizingcell.bounds = cgrectmake(0.0f, 0.0f, cgrectgetwidth(self.tableview.frame), cgrectgetheight(sizingcell.bounds)); [sizingcell setneedslayout]; [sizingcell layoutifneeded]; cgsize size = [sizingcell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize]; return size.height + 1.0f; } -(cgfloat)tableview:(uitableview *)tableview estimatedheightforrowatindexpath:(nsindexpath *)indexpath{ return 155.0f; }
edit 1
here picture of running on ios 7 (which working)
and log output
2015-06-01 18:43:40.855 facts[62233:607] number of rows: 2 2015-06-01 18:43:43.996 facts[62233:607] bounds before layout {{0, 0}, {256, 82}} 2015-06-01 18:43:55.068 facts[62233:607] content view before layout {{0, 0}, {256, 82}} 2015-06-01 18:44:09.409 facts[62233:607] bounds after layout {{0, 0}, {256, 82}} 2015-06-01 18:44:12.843 facts[62233:607] content view before layout {{0, 0}, {256, 82}} 2015-06-01 18:44:21.462 facts[62233:607] bounds before layout {{0, 0}, {256, 82}} 2015-06-01 18:44:23.884 facts[62233:607] content view before layout {{0, 0}, {256, 82}} 2015-06-01 18:44:30.536 facts[62233:607] bounds after layout {{0, 0}, {256, 82}} 2015-06-01 18:44:32.278 facts[62233:607] content view before layout {{0, 0}, {256, 82}}
from piece of code:
-(cgfloat)calculateheightforconfiguredsizingcell:(uitableviewcell *)sizingcell{ sizingcell.bounds = cgrectmake(0.0f, 0.0f, cgrectgetwidth(self.tableview.frame), cgrectgetheight(sizingcell.bounds)); nslog(@"bounds before layout %@", nsstringfromcgrect(sizingcell.bounds)); nslog(@"content view before layout %@", nsstringfromcgrect(sizingcell.contentview.bounds)); [sizingcell setneedslayout]; [sizingcell layoutifneeded]; cgsize size = [sizingcell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize]; nslog(@"bounds after layout %@", nsstringfromcgrect(sizingcell.bounds)); nslog(@"content view before layout %@", nsstringfromcgrect(sizingcell.contentview.bounds)); return size.height + 1.0f; }
but on ios8 see:
and output:
2015-06-01 18:47:14.688 facts[62306:81355636] bounds before layout {{0, 0}, {256, 44}} 2015-06-01 18:47:14.688 facts[62306:81355636] content view before layout {{0, 0}, {256, 44}} 2015-06-01 18:47:14.688 facts[62306:81355636] bounds after layout {{0, 0}, {256, 44}} 2015-06-01 18:47:14.688 facts[62306:81355636] content view before layout {{0, 0}, {256, 44}}
the sizingcell in ios7 reading correct bounds cell's contents in ios8 not.
you're doing much; apple can handle leading , trailing spacing you.
0 is exactly right number use, pin superview.margin
, not superview
. apple adjust margin recommended margin, dependent on device. (wider on 4.7", narrower on 4".)
fyi, 0 times multiplier factor 0, current constraints won't expect should.
the reason why cell height off due misplaced frame (that interface builder couldn't warn because label constraints not installed anyxany).
ios 7 still solve cell height, ios 8 requires cell height correct (solvable).
Comments
Post a Comment