为什么我的TableView单元格比其内容更大(更高)? (使用自动布局)

时间:2014-06-13 15:29:41

标签: ios uitableview ios7 autolayout

这让我发疯了。我有一个tableview,在每个单元格中,顶部有一个简单的UILabel,下面是一行图像。像这样:

I want the label to be smaller in height. I want it wrapped around the text.

以下是我对TableView控制器的一些代码:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // This project has only one cell identifier, but if you are have more than one, this is the time
    NSString *reuseIdentifier = cellIdentifier;

    MyCell *cell = [self.offscreenCells objectForKey:reuseIdentifier];
    if (!cell) {
        cell = [[MyCell alloc] init];
        [cell setDelegate:self];
        [self.offscreenCells setObject:cell forKey:reuseIdentifier];
    }

    [cell.titleLabel setText:@"UILabel - I want it smaller (wrapped)"];
    NSString *url = @"http://assets.bizjournals.com/seattle/news/soccer%20ball%20square*304.jpg?v=1";
    [self.imageView1 loadImageForURLString:url];
    [self.imageView2 loadImageForURLString:url];
    [self.imageView3 loadImageForURLString:url];
    [self.imageView4 loadImageForURLString:url];

    // Make sure the constraints have been added to this cell, since it may have just been created from scratch
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));

    // Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints
    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    // Get the actual height required for the cell
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    // Add an extra point to the height to account for the cell separator, which is added between the bottom
    // of the cell's contentView and the bottom of the table view cell.
    height += 1;

    return height;
}

这是我的tableview单元格中的一些相关代码:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [self.titleLabel setNumberOfLines:1];
        [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [self.titleLabel setTextColor:[UIColor blackColor]];

        [self.imagesView addSubview:self.titleLabel];
        [self.imagesView addSubview:self.imageView1];
        [self.imagesView addSubview:self.imageView2];
        [self.imagesView addSubview:self.imageView3];
        [self.imagesView addSubview:self.imageView4];

        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.imagesView];

        self.contentView.backgroundColor = [UIColor whiteColor];
        [self.titleLabel setBackgroundColor:[UIColor greenColor]];
        [self.imagesView setBackgroundColor:[UIColor blueColor]];

        [self.contentView setNeedsLayout];
        [self.contentView layoutIfNeeded];
    }
    return self;
}

- (void)updateConstraints
{
    [super updateConstraints];

    if (self.didSetupConstraints) {
        return;
    }

    [UIView autoSetPriority:UILayoutPriorityRequired forConstraints:^{
        [self.titleLabel autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
    }];

    [self.titleLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:kLabelVerticalInsets relation:NSLayoutRelationEqual];
    [self.titleLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:kLabelHorizontalInsets];
    [self.titleLabel autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:kLabelHorizontalInsets];

    [UIView autoSetPriority:UILayoutPriorityRequired forConstraints:^{
        [self.imagesView autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
    }];

    [self.imagesView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationLessThanOrEqual];
    [self.imagesView autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:0 relation:NSLayoutRelationEqual];
    [self.imagesView autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:0 relation:NSLayoutRelationEqual];
    [self.imagesView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];

    NSArray *subviews = @[self.imageView1, self.imageView2, self.imageView3, self.imageView4, self.imageView5];
    [self.imageView1 autoMatchDimension:ALDimensionHeight toDimension:ALDimensionWidth ofView:self.imageView1];

    [subviews autoMatchViewsDimension:ALDimensionHeight];
    [subviews autoDistributeViewsAlongAxis:ALAxisHorizontal withFixedSpacing:10.0f insetSpacing:NO alignment:NSLayoutFormatAlignAllCenterY];
    [subviews autoMatchViewsDimension:ALDimensionHeight];

    [self.imageView1 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationEqual];
    [self.imageView2 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationEqual];
    [self.imageView3 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationEqual];
    [self.imageView4 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationEqual];
    [self.imageView5 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets relation:NSLayoutRelationEqual];

    // Pin the images in the container so they appear
    [self.imageView1 autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
    [self.imageView1 autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];

    self.didSetupConstraints = YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    // Make sure the contentView does a layout pass here so that its subviews have their frames set, which we
    // need to use to set the preferredMaxLayoutWidth below.
    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];

    self.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.titleLabel.frame);
}

所以我的问题是:为什么UILabel不包装我的文字?我是否以正确的方式固定观点?

0 个答案:

没有答案