2015-12-02 61 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

相關問題