问题
I develop an iOS app with Swift and storyboard. I placed a table and I need to change table cell height depending on screen class, but I can't find such option in table options. I see that cell height is defined by Table View / Row height option, but it doesn't support class sizes.
This problem happens because I set fonts of labels in the cell prototype using size classes, so for bigger screens fonts are bigger, but row height can't be changed per size class. I need a higher row for bigger fonts, obviously. What approach should I use to achieve it?
Obj-C code will do too.
回答1:
Let me answer my own question - in iOS 8 it's possible to enable auto layout for cells using UITableViewAutomaticDimension for table's row height:
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 89
tableView.rowHeight = UITableViewAutomaticDimension
}
estimatedRowHeight needs to be set too.
Links:
http://natashatherobot.com/ios-8-self-sizing-table-view-cells-with-dynamic-type/
http://www.appcoda.com/self-sizing-cells/
https://stackoverflow.com/a/28918600/1028256
回答2:
Set it by code using the the UITableView Delegate.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//check the conditions and return the row height.
return 71.0;
}
You can find the similar delegate in Swift also.
来源:https://stackoverflow.com/questions/33051556/size-class-for-table-row-height-in-ios-storyboard