iOS7.0 und iOS 7.1 berücksichtigen die dynamische Tabellenansicht nich
Ich habe Autolayout in verschiedenen Implementierungen für UITableViewCell verwendet, wobei der Ansatz darin besteht, dass die intrinsische Größe die Größe definiert und die Höhe für Tabellenansichtszeilen bereitstellt.
Seltsamerweise funktioniert das Targeting von iOS7 und höher mit Autolayout in UITableViewCell nicht wie gewünscht.
Um eines der anderen Probleme zu beheben, bei denen Tabellenzellen zusammengedrückt wurden (unter iOS8 und höher), fügte ich Folgendes hinzu: check
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 ){
self.tblvChatList.rowHeight = UITableViewAutomaticDimension;
self.tblvChatList.estimatedRowHeight = 44;
}
Die einzigen anderen Methoden für die Tabellenansicht, die ich verwendet habe, sind
- (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;
}
Ich habe auch nicht verwendetestimateHeightForRowAtIndexPath:
oderheightForRowAtIndexPath:
und lassen Sie den Inhalt die Höhe für tableviewRow bestimmen.
Warum werden die tableView-Zellen unter iOS7, iOS7.1 (Simulator und Geräte) alle zusammengedrückt, aber unter iOS8 und höher werden dieselben Inhalte für dieselben Inhalte perfekt angezeigt.
Um dieses Quetschen zu beheben, wenn ich @ implementieestimatedHeightForRowAtIndexPath:
dann nicht implementierenheightForRowAtIndexPath:
führt unter iOS7 zu einem Absturz. Gibt es eine Möglichkeit, TableView die Zeilenhöhe aus dem Autolayout-System selbst ableiten zu lassen (Abgeleitet von der inhärenten Inhaltsgröße von UILabel, wobei zusätzliche Auffüllungen berücksichtigt werden, da ich die erforderlichen Ränder hinzugefügt habe)?
Die Autolayout-Einschränkungen für UILabel in der Zelle lauten wie folgt: