I have a table view with dynamically sized cells. This works great in iOS 8 but it's not supported in iOS 7. I'm required to implement tableView:heightForRowAtIndexPath or the app will crash in iOS 7. If I do that, I'm going to have to calculate the height of the cell. The problem is that if I implement this method, iOS 8 pays attention to it and no longer does its dynamically sized cell magic. Is there any way to only implement this method for iOS 7 clients?
I have used something similar to this before:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:@"CellIdentifier"];
// Stuff & Things
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
return [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}
The new internal autolayout self-sizing cells from iOS 8 cannot magically be reflected back to iOS 7. But that's not a problem. If you want to be backwards-compatible to iOS 7, then, as you rightly say, this means that you will determine the cell height yourself, so that you can return the correct value from tableView:heightForRowAtIndexPath:
. This is what we did in iOS 4, 5, 6, and 7. That will still work in iOS 8 too (and is probably faster anyway).
So my advice would be: just write the code as if for iOS 7. If you're coding in Objective-C you could have two different sets of code (conditional compilation), and even in Swift you could use two different classes, depending on what system we find ourselves on; but I regard that as a bit over the top - it's a code maintenance nightmare waiting to happen.
I got UIViewAlertForUnsatisfiableConstraints
error in debug log.
Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "" )
If there is no tableView:heightForRowAtIndexPath:
, the cell looks fine. So, I want to disable it in iOS 8. @batu gave a solution in another post: Implement heightForRowIndexPath: in iOS 7, but remove it in iOS 8 using compile-time macro . Thanks to @batu, the solution is as simple as this:
#define IS_IOS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
-(CGFloat) tableView: (UITableView * ) tableView heightForRowAtIndexPath: (NSIndexPath * ) indexPath {
if (IS_IOS_8_OR_LATER) {
return UITableViewAutomaticDimension;
}
// Your iOS 7 code here.
}
However , my cell still has problem with the height in iOS 8 with iPhone 6. The causes seems to be the fixed width of UILabel (which is designed for 320pt). Here is what I did after a long night research:
I applied the solution from this guide: Dynamic Table View Cell Height and Auto Layout .
Since the UILabel requires explicit width for iOS 7 device, I programmatically "clear" this settings for iOS 8. In my extended table view cell:
- (void)awakeFromNib { // Initialization code if (IS_IOS_8_OR_LATER) { self.descriptionLabel.preferredMaxLayoutWidth = 0; } }
I used the RWLabel
suggestion as from the guide.
I disable return UITableViewAutomaticDimension;
. Whether enable or disable, may depends on the subviews and constraints in table view cell. Try to fit your case.
I change the priority of the Trailing Space for all RWLabel to 999 (instead of 1000). That eliminates the unsatisfiable constraints warning.
Hopefully these solutions would help someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.