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