如何修改tableView中单元格内UIView的大小?

时间:2014-03-14 22:11:27

标签: ios objective-c uitableview uiview

我希望我的单元格具有UIView(用作包含颜色的简单栏),具有特定颜色,用于表格视图中的8个不同的行。 问题是在方法cellForRowAtIndexPath中,我所做的更改不会显示在模拟器中。我只想使条形变量的宽度。所以这是我的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {

static NSString *CellIdentifier = @"cellR";
ResumenCelda *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...

//dictSR is a dictionary where I store some data

NSString *key = [self.dictSR allKeys][indexPath.row];

NSString *sum = self.dictSR[key];

cell.categoria.text  = key;
cell.montoTotal.text  = sum;

//the function "calcularBarra" is implemented down below

float w = [self calcularBarra:[sum floatValue]];
NSLog(@"VALOR BARRA : %f",w);

[cell.barra setFrame:CGRectMake(129.0f, 25.0f, w, 50.0f)];  // <--- This line doesn't work!
[cell.barra setBackgroundColor:[UIColor grayColor]];
//[cell.contentView addSubview:cell.barra];  Could I omit this line of code if I already had a UIView in my Storyboard tied to "cell.barra" ? 

return cell;
  }

-(int)calcularBarra:(float)a

{
int widthB = (100*a)/max;     //max is declared in myViewController.h as @property float max;
return widthB;
}

在我的viewDidLoad方法中,我计算最大值:

- (void)viewDidLoad
{
[super viewDidLoad];

for (int i=0; i<8; i++) {
    NSString *key = [self.dictSR allKeys][i];
    float b=[dictSR[key] floatValue];
    if( b > max )
    {
        max=b;
    }
}
NSLog(@"EL MAXIMO ES %f",max);

// Do any additional setup after loading the view.
}

我没有使用UIView,而是尝试使用UIButton。 希望您能够帮助我。 感谢。

1 个答案:

答案 0 :(得分:2)

可能是自动布局,请检查您的单元格是否已禁用。

enter image description here