更改 TableCell 中 UIView 的大小

change the size of a UIView in a TableCell

我有一个tableCell。在单元格中,我有一个带有背景颜色的 UIVIew。

我想根据数据改变UIView的宽度。

cellForRowAtIndexPath 中,我添加了以下行来更改视图的宽度。

//set the frame of the bar
CGRect  rect = cell.barView.frame;
rect.size.width = myViewWidth;
cell.barView.frame = rect;

这对设置名为 barView 的 UIVIew 的宽度没有影响。

barView 在单元格中设置了以下约束。

  1. 领先 Space 到 SuperView = 0
  2. 底部 Space 到 SuperView = 0
  3. 身高=18
  4. 宽度 >=0

如何调整条形视图的大小?

P.s。当代码在 cellForROwAtIndexPath

中执行时,我已验证 myViewWidth 大于 0

为视图的宽度创建约束,并link为单元格内的 IBOutlet 创建约束。

@interface historicoTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lauguraFaixa;

在 cellForRowAtIndexPath 中根据需要更改此约束。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    historicoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[historicoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    int duracao = [self duracao:[[self.periodos objectAtIndex:indexPath.row] inicio] data:[[self.periodos objectAtIndex:indexPath.row] fim]];

    int diasMaximos = 9;
    int larguraMinima = 100;
    int larguraMaxima = 300;
    int largura = larguraMinima+((larguraMaxima-larguraMinima)/diasMaximos * duracao);

    cell.lauguraFaixa.constant = largura;


    return cell;
}