ios - Is it possible to have different table view cell row heights for different size classes? -
i developing app adaptive layout , works far except need have adaptive table view cell heights. using storyboard , not see way this. mission impossible?
if i'm understanding question correctly, want have cell height adjust different font sizes using size classes; have bigger font on bigger size screen cause cells adjust accordingly. think way have different text views installed particular size classes.
i made test app demonstrates this. in storyboard, added table view controller custom cell. changed size class wcompact hany
, added text view (iboutlet: labelphone) cell. pinned 4 sides of cell, disabled scrolling, made text red, , sized font 8. changed size class wregular hany
, , added text view (iboutlet: labelpad) cell same constraints , green text of font size 30. code in table view controller follows,
- (void)viewdidload { [super viewdidload]; self.tableview.rowheight = uitableviewautomaticdimension; self.tableview.estimatedrowheight = 50; } -(void)viewdidappear:(bool)animated { [super viewdidappear:animated]; [self.tableview reloaddata]; // necessary have cells size correctly when table view first appeared } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return 5; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { rdcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; nsstring *thetext = @"lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non proident, sunt"; cell.labelphone.text = thetext; cell.labelpad.text = thetext; return cell; }
when run on iphone simulator, got small red text, , when run on ipad simulator, got large green text. put sample app here, http://jmp.sh/wfez8pi
after edit:
actually, found easier way. can add subview (text view or label) using default wany hany
size class (you don't need 2 different views). in attributes inspector, next font field, there's little "+" button. click on that, , can add different font sizes different size classes. that's need do. cellforrowatindexpath becomes this,
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { rdcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; cell.textview.text = self.thedata[indexpath.row]; return cell; }
Comments
Post a Comment