iOS7.0 и iOS 7.1 не соблюдают динамическую высоту таблицы
Я использовал autolayout в различных реализациях для UITableViewCell, подход, позволяющий внутреннему размеру определять размер и который в свою очередь обеспечит высоту для строк табличного представления.
Странно, но ориентация на iOS7 и выше с Autolayout в UITableViewCell не работает должным образом.
Чтобы исправить одну из других проблем, когда ячейки табличного представления стали сжатыми (на iOS8 и выше), я добавил следующую проверку
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 ){
self.tblvChatList.rowHeight = UITableViewAutomaticDimension;
self.tblvChatList.estimatedRowHeight = 44;
}
Единственными другими методами просмотра таблиц, которые я использовал, являются
- (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;
}
Я тоже не пользоваласьestimateHeightForRowAtIndexPath:
или жеheightForRowAtIndexPath:
и пусть содержимое решает высоту для tableviewRow.
Почему все ячейки tableView сжимаются при работе на iOS7, iOS7.1 (Симулятор и устройства), но отлично отображаются на iOS8 и выше для того же контента для того же контента.
Чтобы исправить это сжатие, если я реализуюestimatedHeightForRowAtIndexPath:
тогда не реализуемheightForRowAtIndexPath:
приводит к падению на iOS7. Есть ли способ позволить tableview выводить высоту строки из самой системы автоматического размещения (как получено из внутреннего размера содержимого UILabel с дополнительным заполнением, учитываемым, поскольку я добавил требуемые поля)?
Ограничения автопоставки для UILabel внутри ячейки следующие