JuanM.
JuanM.

Reputation: 432

How to modify the size of a UIView within a cell in a tableView?

I want my cells to have a UIView (functioning as a simple bar containing a color) with a specific color, for 8 different rows in my table view. The thing is that in the method cellForRowAtIndexPath, the changes I made are not displayed in the simulator. I only want to make the width of the bar variable. So here is my code:

-(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;
}

In my viewDidLoad method I calculate the maximum value:

- (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.
}

Instead of using an UIView, I also tried with a UIButton. Hope you can help me. Thanks.

Upvotes: 0

Views: 68

Answers (1)

MCMatan
MCMatan

Reputation: 8843

Its probably the Auto layout, check that it is disabled on your cell.

enter image description here

Upvotes: 2

Related Questions