UITableViewCell systemLayoutSizeFittingSize:在iOS 7上返回0

Imo*_*tep 6 uitableview ios autolayout ios7.1 ios8.1

我有一个UITableViewCell,UIImage固定在左上角,右边有一个标签:

    -(UITableViewCell*) adCellFromTableView:(UITableView*)tableView
{
    //Build the text
    NSString* adText = NSLocalizedString(@"MyFamily_fremiumAd", nil);
    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:adText];

    NSUInteger newLineLocation = [adText rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location;

    //Set the first line in orange
    NSDictionary* firstLineAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],
                                          NSForegroundColorAttributeName:ORANGE};
    [attrString addAttributes:firstLineAttributes range:NSMakeRange(0, newLineLocation)];
    //Set other lines in white
    NSDictionary* otherLinesAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:11],
                                           NSForegroundColorAttributeName:[UIColor whiteColor]};
    [attrString addAttributes:otherLinesAttributes range:NSMakeRange(newLineLocation, adText.length - newLineLocation)];

    //Get the cell
    if (!adCell)
    {
        adCell = [tableUsers dequeueReusableCellWithIdentifier:@"fremiumAd"];
    }

    //Set the text
    UILabel* label = (UILabel*)[adCell viewWithTag:1];
    label.attributedText = attrString;

    //Hide the separator
    adCell.separatorInset = UIEdgeInsetsMake(0, adCell.bounds.size.width, 0, 0);

    return adCell;
}


-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [self adCellFromTableView:tableView];

            //Make sure the cell's bounds are the same as tableview's before calculating the height
            //This is important when we calculate height after a UI rotation.
            cell.bounds = CGRectMake(0, 0, tableView.bounds.size.width, 0);
            NSLog(@"cell.bounds: %@",NSStringFromCGRect(cell.bounds));
            [cell setNeedsLayout];
            [cell layoutIfNeeded];

            CGSize fittingSize = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
            NSLog(@"fittingSize: %@",NSStringFromCGSize(fittingSize));
            return fittingSize.height;
}
Run Code Online (Sandbox Code Playgroud)

当我在iOS 7.1模拟器上运行应用程序时,systemLayoutSizeFittingSize始终返回0:

cell.bounds:{{0,0},{320,0}}

fittingSize:{0,0}

当我在iOS 8.1模拟器上运行应用程序时,systemLayoutSizeFittingSize返回一个正确的值:

cell.bounds:{{0,0},{320,0}}

fittingSize:{320,154.5}

我错过了什么?

编辑: 我有点用[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];而不是修复问题[cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

但这仅仅是一个修复:当我在单元格可见时旋转时,尺寸计算正常.但是当我将单元格滚出屏幕时,旋转UI然后向后滚动到单元格,iOS 7.1中的高度计算再次出错

以下是从横向旋转到纵向之前的日志:

tableView bounds:{{0,0},{569,227}}

cell.bounds:{{0,0},{569,0}}

cell.contentView:{{0,0},{569,0}}

fittingSize:{559,162}

以下是从横向旋转到纵向后的日志:

tableView bounds:{{0,249},{321,463}}

cell.bounds:{{0,0},{321,0}}

cell.contentView:{{0,0},{321,0}}

fittingSize:{559,162}

如您所见,无论cell/cell.contentView的宽度是多少,大小计算都是相同的.

这会导致从纵向旋转到横向时的超大单元格,以及从横向旋转到纵向时尺寸过小的单元格.

董嘉伟*_*董嘉伟 0

您是对的,在 systemLayoutSizeFittingSize: 方法中使用 cell.contentView 而不是 cell 。使用自动布局,所有自定义子视图只能添加到单元格的 contentView 以及约束中。然后自动布局单元格就可以按照您的预期正常工作。

作为你的第二个问题,我已经解决了类似的问题。在 -(void) didRotateFromInterfaceOrientation :(UIInterfaceOrientation)fromInterfaceOrientation 方法中插入以下代码也许可以帮助您解决此问题。

-(void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation
{
    // Update the layout for the new orientation
    //Maybe you should change it to your own tableview
      [self updateViewConstraints]; 
      [self.view layoutIfNeeded];
     // other any code 
    ...

}
Run Code Online (Sandbox Code Playgroud)