iOS7.0和iOS 7.1不支持动态tableview高度

时间:2015-04-03 03:26:43

标签: objective-c uitableview ios7 autolayout ios7.1

我在UITableViewCell的各种实现中使用了autolayout,方法是让内在大小定义大小,然后为tableview行提供高度。

奇怪的是,在UITableViewCell中使用Autolayout定位iOS7及更高版本并不能正常工作。

enter image description here

要修复tableview单元被挤压的其他问题之一(在iOS8及以上版本),我添加了以下检查

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1 ){
    self.tblvChatList.rowHeight =  UITableViewAutomaticDimension;
    self.tblvChatList.estimatedRowHeight = 44;
}

我使用的唯一其他tableview方法是

- (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从autolayout系统本身推断行高(从UILabel的内部内容大小派生,并考虑到额外的填充,因为我已经添加了所需的边距)?

单元格内UILabel的autolayout约束如下

enter image description here

0 个答案:

没有答案