I have used autolayout in various implementations for UITableViewCell, the approach being let the intrinsic size define the size and which in turn will provide height for tableview rows.
Strangely but targeting iOS7 and above with Autolayout in UITableViewCell isn't working as desired.
To fix one of the other issues where tableview cells became squeezed (on iOS8 and above) I added following check
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 ){
self.tblvChatList.rowHeight = UITableViewAutomaticDimension;
self.tblvChatList.estimatedRowHeight = 44;
}
The only other tableview methods that I have used are
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([self.arrDataSource count] > ZERO_VALUE) {
self.tblvChatList.backgroundView = nil;
self.tblvChatList.separatorStyle = UITableViewCellSeparatorStyleNone;
return [self.arrDataSource count];
}
else {
// Display a message when the table is empty
if (!viewTableBackground) {
self.viewTableBackground = [[UIView alloc] initWithFrame:self.tblvChatList.bounds];
UIImageView* imgConChatLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IconConChatTheme"]];
[imgConChatLogo setContentMode:UIViewContentModeCenter];
[imgConChatLogo setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel* lblNoListDataError = [[UILabel alloc] initWithFrame:CGRectMake(ZERO_VALUE, ZERO_VALUE,
(2*self.view.bounds.size.width)/3, self.view.bounds.size.height)];
lblNoListDataError.text = NSLocalizedString(@"STR_WRITE_SOMETHING_TO_MATCH",
@"No data is currently available. Please pull down to refresh.");
[lblNoListDataError setTranslatesAutoresizingMaskIntoConstraints:NO];
lblNoListDataError.textColor = [UIColor lightGrayColor];
lblNoListDataError.numberOfLines = ZERO_VALUE;
lblNoListDataError.textAlignment = NSTextAlignmentCenter;
lblNoListDataError.font = [Theme fontForRegularBody];
[lblNoListDataError sizeToFit];
[viewTableBackground addSubview:lblNoListDataError];
[viewTableBackground addSubview:imgConChatLogo];
[viewTableBackground addConstraint:
[NSLayoutConstraint constraintWithItem:imgConChatLogo
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:viewTableBackground
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[viewTableBackground addConstraint:
[NSLayoutConstraint constraintWithItem:lblNoListDataError
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:viewTableBackground
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
[viewTableBackground addConstraint:
[NSLayoutConstraint constraintWithItem:imgConChatLogo
attribute:NSLayoutAttributeBaseline
relatedBy:NSLayoutRelationEqual
toItem:viewTableBackground
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:-10]];
[viewTableBackground addConstraint:
[NSLayoutConstraint constraintWithItem:lblNoListDataError
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:viewTableBackground
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:10]];
}
self.tblvChatList.backgroundView = self.viewTableBackground;
self.tblvChatList.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return ZERO_VALUE;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = nil;
NSUInteger userId = [[NSUserDefaults standardUserDefaults] integerForKey:@"USER_ID"];
ChatMessage* message = (ChatMessage*)[self.arrDataSource objectAtIndex:indexPath.row];
if (userId == [message messageSenderId]) {
static NSString* reuseIdentifierReceiverCell = @"ChatReceiverCustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifierReceiverCell forIndexPath:indexPath];
[(ChatReceiverCell*)cell showMessage:message];
}
else {
static NSString* reuseIdentifierSenderCell = @"ChatSenderCustomCell";
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifierSenderCell forIndexPath:indexPath];
// Configure the cell...
[(ChatSenderCell*)cell showMessage:message];
}
return cell;
}
I haven't used either estimateHeightForRowAtIndexPath:
or heightForRowAtIndexPath:
and let the content decide the height for tableviewRow.
- Issue:
Why the tableView cells are all squeezed up when running on iOS7, iOS7.1 (Simulator and devices) but show perfectly on iOS8 and above for same content for the same content.
To fix this squeezing, If I implement estimatedHeightForRowAtIndexPath:
then not implementing heightForRowAtIndexPath:
results in a crash on iOS7. Is there are way to let tableview infer the row height from the autolayout system itself (As derived from intrinsic content size of UILabel with extra padding kept in consideration as I have added required margins) ?
The autolayout constrains for UILabel inside the cell are as follows
来源:https://stackoverflow.com/questions/29425782/ios7-0-and-ios-7-1-doesnt-honor-dynamic-tableview-height