Objective c 使用自定义字体的UITableViewCell的动态高度
我一直在尝试制作一个用于聊天的动态表视图单元格 你可以看到,实际上我使用了一个固定高度的单元格(88),但如果文本更长,那就太糟糕了 我使用的是Muli字体,这就是为什么我不知道如何计算正确的高度 这是我的密码: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
- (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
中设置一次表视图的行高