Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用自定义字体的UITableViewCell的动态高度_Objective C_Uitableview_Height_Heightforrowatindexpath - Fatal编程技术网

Objective c 使用自定义字体的UITableViewCell的动态高度

Objective c 使用自定义字体的UITableViewCell的动态高度,objective-c,uitableview,height,heightforrowatindexpath,Objective C,Uitableview,Height,Heightforrowatindexpath,我一直在尝试制作一个用于聊天的动态表视图单元格 你可以看到,实际上我使用了一个固定高度的单元格(88),但如果文本更长,那就太糟糕了 我使用的是Muli字体,这就是为什么我不知道如何计算正确的高度 这是我的密码: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (CGFloat)tableView:(UITable

我一直在尝试制作一个用于聊天的动态表视图单元格 你可以看到,实际上我使用了一个固定高度的单元格(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;

}

谢谢

表格视图:heightForRowAtIndexPath:
方法中的
88
替换为您希望特定行具有的高度,使用类似的方法进行计算。

仅供参考-如果您有固定行高的表格视图,请不要实现
heightForRowAtIndexPath:
方法。而是在
viewDidLoad
中设置一次表视图的
行高