Ios 自定义UITableViewCell:如何正确实现布局子视图?

Ios 自定义UITableViewCell:如何正确实现布局子视图?,ios,objective-c,xcode,uitableview,autolayout,Ios,Objective C,Xcode,Uitableview,Autolayout,我有自定义UITableViewCell,其中我覆盖了layoutSubview,并手动计算了子视图的坐标。它起作用了。。但只有当我使用滚动或单击单元格时,单元格才会更新。此外,当我更改方向时,单元格将保持不变,不进行更新 我已经尝试对子视图使用自动布局。它工作正常,单元格按预期更新:方向也发生变化 但是。。我想使用布局子视图。。IB几十年的限制让人头疼。。就我而言 那么,我应该如何处理layoutSubviews以获得自定义单元格的预期行为呢 我尝试使用不同的更新,这些都是在其他问题中建议的,

我有自定义UITableViewCell,其中我覆盖了
layoutSubview
,并手动计算了子视图的坐标。它起作用了。。但只有当我使用滚动或单击单元格时,单元格才会更新。此外,当我更改方向时,单元格将保持不变,不进行更新

我已经尝试对子视图使用自动布局。它工作正常,单元格按预期更新:方向也发生变化

但是。。我想使用
布局子视图
。。IB几十年的限制让人头疼。。就我而言

那么,我应该如何处理
layoutSubviews
以获得自定义单元格的预期行为呢

我尝试使用不同的更新,这些都是在其他问题中建议的,但没有人解决我的问题

这是我的密码:

- (void)layoutSubviews
{
    [super layoutSubviews];

    NSLog(@"------ cell layout subviews ------");

    //self.selectedBackgroundView.frame = CGRectMake(10.0f, 0, 300, 80);

    CGRect frame = basketButton.frame;
    int parentX1 = 0;
    int parentY1 = 0;
    int parentX2 = self.contentView.bounds.size.width;
    int parentY2 = self.contentView.bounds.size.height;

    NSLog(@"parentX2: %d", parentX2);
    NSLog(@"contentView: %f", self.contentView.frame.size.width);

    int x, y, h, w;
    int gap = 10;

    w = 80;
    h = 30;
    x = parentX2 - gap - w;
    y = parentY2/2 - h/2;
    frame = CGRectMake( x, y, w, h );
    basketButton.frame = frame;

    //NSLog(@"basckeButtonX2: %f", frame.origin.x);

    w = 60;
    h = 15;
    x = frame.origin.x - gap - w;
    y = parentY2/2 - h/2;
    frame = CGRectMake( x, y, w, h );
    priceLabel.frame = frame;

    w = 72;
    h = 72;
    x = gap;
    y = gap;
    frame = CGRectMake(x, y, w, h);
    imageView.frame = frame;

    x = imageView.frame.origin.x + imageView.frame.size.width + gap;
    y = gap;
    w = priceLabel.frame.origin.x - gap - x;
    h = parentY2 - gap*2;
    frame = CGRectMake(x, y, w, h);
    nameLabel.frame = frame;
}
在控制器中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"ProductCell";

    ProductCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCell"];

    NSArray* productsByTableView = [self getProductsByTableView: tableView];
    Product* product = [productsByTableView objectAtIndex:indexPath.row];
    cell.nameLabel.text = product.name;

    CGRect frame = cell.frame;
    frame.size.width = tableView.frame.size.width;
    cell.frame = frame;

    [cell setNeedsLayout];
    [cell setNeedsDisplay];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCell"];

    CGRect frame = cell.frame;
    frame.size.width = tableView.frame.size.width;
    cell.frame = frame;

    [cell setNeedsLayout];
    [cell setNeedsDisplay];
    //[cell layoutSubviews];

    return cell.bounds.size.height;
}