0

I've been trying to make a dynamic table view cell to use in a chat You can see that actually im using a constant height for the cell(88) but if the text is longer, it will be awful

I'm using the font Muli and that's why I don't know how can I calculate the correct height.

here is my code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 88;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
return [self.comunidades count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[self.tabla dequeueReusableCellWithIdentifier:@"DM_tab"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DM_tab" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }

   NSDictionary *comunidad = [self.comunidades objectAtIndex:indexPath.row];

UILabel *textosender=(UILabel *) [cell viewWithTag:2];
textosender.text=[comunidad objectForKey:@"nombre"];
textosender.font = [UIFont fontWithName:@"Muli-Light" size:14];
UILabel *textomio= (UILabel *) [cell viewWithTag:3];
textomio.text=[comunidad objectForKey:@"nombre"];
textomio.font = [UIFont fontWithName:@"Muli-Light" size:14];
NSString *tipo=[comunidad objectForKey:@"sender"];

NSUserDefaults *defs=[NSUserDefaults standardUserDefaults];

if([tipo isEqualToString:[defs objectForKey:@"username"]]){
    UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"A_DM_fondo@2x.png"]];
    [cell setBackgroundView:img];
    textosender.hidden=YES;
    textomio.hidden=NO;
    NSString *detallemio=[comunidad objectForKey:@"mensaje"];
    [textomio setText:detallemio];
}
else{
    UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"N_DM_fondo@2x.png"]];
    [cell setBackgroundView:img];
    textosender.hidden=NO;
    textomio.hidden=YES;
    NSString *detallemio=[comunidad objectForKey:@"mensaje"];
    [textosender setText:detallemio];
}

return cell;

}

Thanks

slozano95
  • 278
  • 7
  • 22
  • 1
    FYI - for future reference, if you ever do have a table view with a fixed row height, do not implement the `heightForRowAtIndexPath:` method. Instead you set the `rowHeight` of the table view once in `viewDidLoad`. – rmaddy May 12 '14 at 22:50
  • see this link http://stackoverflow.com/questions/19215199/dynamically-size-uitableviewcell-according-to-uilabel-with-paragraph-spacing/19217965#19217965 – Shankar BS May 13 '14 at 05:08

1 Answers1

1

Replace the 88 in the tableView:heightForRowAtIndexPath: method with the height you want the particular row to have, calculated using something like the boundingRectWithSize:options:attributes:context: method on NSString.

Isaac
  • 10,668
  • 5
  • 59
  • 68