0

我一直在尝试制作一个动态表格视图单元格以在聊天中使用您可以看到实际上我使用单元格的恒定高度(88)但是如果文本更长,那将会很糟糕

我使用的是 Muli 字体,这就是为什么我不知道如何计算正确的高度。

这是我的代码:

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

}

谢谢

4

1 回答 1

1

88将方法中的替换为tableView:heightForRowAtIndexPath:您希望特定行具有的高度,使用类似onboundingRectWithSize:options:attributes:context:方法NSString计算。

于 2014-05-12T22:13:12.750 回答