2015-12-02 53 views
0

我已经创建了一个具有IMAGE视图的自定义单元格,并且两个标签的标签数据都是从plist文件填充的,数据正确填充,但在单元格的前端没有填充没有正确显示数据,标签切割的数据。我正在使用Uilabel视图。根据标签文本+图像计算单元格高度

请查看我的代码,我通过互联网搜索,并遵循一些教程,但没有任何工作。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    Customviewcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    UIImage * image = [UIImage imageNamed:justThumbs[indexPath.row]]; 
    cell.CustomTitle.text=justTitles[indexPath.row]; 

    cell.CustomTitle.numberOfLines =0; 
    [cell.CustomTitle sizeToFit]; 
    cell.CustomDes.text=justDesc[indexPath.row]; 
    cell.CustomDes.numberOfLines=0; 
     [cell.CustomDes sizeToFit]; 
    [cell.CustomTitle layoutIfNeeded]; 
    [cell.CustomDes layoutIfNeeded]; 
    [cell layoutIfNeeded]; 
    cell.Customimage.image=image; 
    return cell; 
} 

用于计算高度的代码根据stackoverflow不同的问题的答案。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Calculate Height Based on a cell 
    if (!self.customcell) { 
     self.customcell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    } 
    // Configure Cell 
    UIImage * image = [UIImage imageNamed:justThumbs[indexPath.row]]; 
    self.customcell.CustomTitle.text=justTitles[indexPath.row]; 
    self.customcell.CustomTitle.numberOfLines=0; 
    [self.customcell.CustomTitle sizeToFit]; 
    self.customcell.CustomDes.text=justDesc[indexPath.row]; 
    self.customcell.CustomDes.numberOfLines=0; 
    [self.customcell.CustomDes sizeToFit]; 
    self.customcell.Customimage.image=image; 

    //Layout Cell 

    //Get Hieght for the cell 

    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) 
    { 

     if ([[UIScreen mainScreen] bounds].size.height == 568) 
     { 

      CGRect frame = [NSString setAttributeWithString:self.customcell.CustomTitle.text withLineSpacing:0.2 withSize:CGSizeMake(270, 999999999) withFont:self.customcell.CustomTitle.font withLabel:self.customcell.CustomTitle setLabelTextColor:self.customcell.CustomTitle.textColor setTextAlignment:self.customcell.CustomTitle.textAlignment]; 
      self.customcell.CustomTitle.height.constant = frame.size.height; 
      frame = [NSString setAttributeWithString:self.customcell.CustomDes.text withLineSpacing:0.3 withSize:CGSizeMake(150, 999999999) withFont:self.customcell.CustomDes.font withLabel:self.customcell.CustomDes setLabelTextColor:self.customcell.CustomDes.textColor setTextAlignment:self.customcell.CustomDes.textAlignment]; 
      self.customcell.CustomDes.height.constant = frame.size.height; 



     } 
     else{ 

      CGRect frame = [NSString setAttributeWithString:self.customcell.CustomTitle.text withLineSpacing:1 withSize:CGSizeMake(337, 999999999) withFont:self.customcell.CustomTitle.font withLabel:self.customcell.CustomTitle setLabelTextColor:self.customcell.CustomTitle.textColor setTextAlignment:self.customcell.CustomTitle.textAlignment]; 
      self.customcell.CustomTitle.height.constant = frame.size.height; 
      frame = [NSString setAttributeWithString:self.customcell.CustomDes.text withLineSpacing:1 withSize:CGSizeMake(227, 999999999) withFont:self.customcell.CustomDes.font withLabel:self.customcell.CustomDes setLabelTextColor:self.customcell.CustomDes.textColor setTextAlignment:self.customcell.CustomDes.textAlignment]; 
      self.customcell.CustomDes.height.constant = frame.size.height; 


     } 
    } 
    [self.customcell layoutIfNeeded]; 

     // CGFloat height = self.customcell.CustomTitle.height.constant+self.customcell.CustomDes.height.constant+189; 
    CGFloat height = [self.customcell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 


    //Add padding of 1 
    return height; 
} 

使用Github开源库来解决问题,但没有奏效。

https://github.com/punchagency/line-height-tool

问题依然存在的,标签的文本切断,内容挂在必和内容是在1000水平垂直+ ..

请帮助..

感谢配发。

+1

您是否在使用autolayout? –

+0

@MGP是的,我正在使用自动布局... – user3015451

+0

@ user3015451图片高度是固定还是动态? – Vvk

回答

1

你可以在NSString的方法boundingRectWithSize一定范围的字符串的高度,就像

NSString* text = @"Test text"; 

CGRect textRect = [text boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX) 
                options:NSStringDrawingUsesLineFragmentOrigin 
                attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} 
                context:nil]; 
CGFloat textHeight = ceilf(textRect.size.height); 

使用自己的字体和字体大小,以及属性字典如果需要,您还可以添加其他属性。

+0

我的内容因每个单元而异... – user3015451

+0

这就是为什么在heightForCellAtIndexPath方法中为每个单元格执行此操作的原因 - 在单元格上定义一个静态方法,该方法将宽度和对象显示在单元格中,并根据对象的内容计算高度。 – TheEye

1

使用下面的方法来计算你的UILabel高度:

附加在你的类也使用此方法。

- (CGFloat)getLabelHeight:(UILabel*)label{ 
CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX); 
CGSize size; 

NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init]; 
CGSize boundingBox = [label.text boundingRectWithSize:constraint 
               options:NSStringDrawingUsesLineFragmentOrigin 
              attributes:@{NSFontAttributeName:label.font} 
               context:context].size; 

size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height)); 

return size.height;} 

更新您的heightForRowAtIndexPath方法按您的要求: 计算所有UI高度和回报。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

Customviewcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; 

CGFloat totalHeight = 0; 

cell.CustomTitle.text=justTitles[indexPath.row]; 
//get label Height 
totalHeight += [Helper getLabelHeight:cell.CustomTitle]; 

cell.Customimage.image = [UIImage imageNamed:justThumbs[indexPath.row]]; 
CGFloat imageHeight = cell.Customimage.frame.size.height; //or Add image height here 

totalHeight += imageHeight; 

return totalHeight;} 
+0

上面的方法并没有奏效,它一直都是一样的高度...... – user3015451

相关问题