2013-10-01 121 views
0

我正在將iOS5項目更新爲storybaord,並且與iOS6/iOS7兼容。我的表格視圖有4個部分,每個部分都有不同長度的數據,這些數據可能會或可能不會顯示爲一條線。單元格高度和換行符

舊的代碼是這樣的一些事情,我得到一個警告的聲明

CGSize size = [productToShow.pDesc sizeWithFont:self.lblHidden.font constrainedToSize:self.lblHidden.frame.size lineBreakMode:NSLineBreakByWordWrapping]; 

警告 sizeWithFont:constrainedToSize:lineBreakMode:is first deprecated in iOS7

所以我一直在使用boundingRectWithSize:options:attributes:context:編譯器的建議嘗試。

但代碼似乎更復雜:蘋果不會讓簡單的事情變得困難。如果有人能看到我的密碼,並提出最好的方法或更好更簡單的方法,那將會對我和社區有幫助。

iOS5的代碼與XIBs

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row]; 

     self.lblHidden.frame = CGRectMake(58, 0, 945, 9999); 
     self.lblHidden.text = tempPointStr; 
     CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font 
           constrainedToSize:self.lblHidden.frame.size 
            lineBreakMode:NSLineBreakByWordWrapping]; 

     if (size.height < 50.0f) 
     { 
      return 50.0f; 
     } 
     else 
     { 
      return size.height; 
     } 

    } 
    elseif(indexPath.section == 1) 
    { 
    ... 
    } 
    else 
    { 
    ... 
    } 

} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     static NSString *tempPoint = @"ProductPointsCell"; 
     ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint]; 
     if (cell == nil) 
     { 
      NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ProductPointsCell_iPad" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
      cell.showsReorderControl = NO; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      cell.backgroundColor=[UIColor clearColor]; 
      cell.lblPoint.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0f]; 
     } 

     NSString *tempPointStr = [self.shortDescArray objectAtIndex:indexPath.row]; 
     cell.lblPoint.text = tempPointStr; 
     self.lblHidden.frame = CGRectMake(58, 0, 945, 9999); 
     self.lblHidden.text = tempPointStr; 
     CGSize size = [tempPointStr sizeWithFont:self.lblHidden.font 
           constrainedToSize:self.lblHidden.frame.size 
            lineBreakMode:NSLineBreakByWordWrapping]; 

     if (size.height < 50.0f) 
     { 
      cell.lblPoint.frame = CGRectMake(58, 0, 945, 50.0f); 
     } 
     else 
     { 
      cell.lblPoint.frame = CGRectMake(58, 0, 945, size.height); 
     } 
    return cell; 
    } 
    elseif(indexPath.section == 1) 
    { 
    ... 
    } 
    else 
    { 
    ... 
    } 
} 

其中在相同的代碼:

iOS6的/ 7碼與storybaord

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     NSString *tempPointStr = (self.shortDescArray)[indexPath.row]; 

     self.lblHidden.frame = CGRectMake(58, 0, 945, 9999); 
     self.lblHidden.text = tempPointStr; 

     CGSize constraint = CGSizeMake(945, 9999.0f); 
     NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0f] forKey:NSFontAttributeName]; 
     NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:self.lblHidden.text attributes:attributes]; 
     CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil]; 
     CGSize size = rect.size; 

     if (size.height < 50.0f) 
     { 
      return 50.0f; 
     } 
     else 
     { 
      return size.height; 
     } 
    } 
    elseif(indexPath.section == 1) 
    { 
    ... 
    } 
    else 
    { 
    ... 
    } 
} 


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     static NSString *tempPoint = @"ProductPointsCell"; 
     ProductPointsCell *cell = (ProductPointsCell*)[tableView dequeueReusableCellWithIdentifier:tempPoint]; 
     cell.showsReorderControl = NO; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.backgroundColor=[UIColor clearColor]; 
     cell.pointLbl.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0f]; 
     [cell.pointLbl setLineBreakMode:NSLineBreakByWordWrapping]; 
     [cell.pointLbl setNumberOfLines:0]; 

     NSString *tempPointStr = (self.shortDescArray)[indexPath.row]; 
     cell.pointLbl.text = tempPointStr; 
     CGSize constraint = CGSizeMake(945,9999.0f); 
     NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:cell.pointLbl.text attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}]; 
     CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil]; 
     CGSize size = rect.size; 

     if (size.height < 50.0f) 
     { 
      cell.pointLbl.frame = CGRectMake(58, 0, 945, 50.0f); 

     } 
     else 
     { 
      cell.pointLbl.frame = CGRectMake(58, 0, 945, size.height); 
     } 
     return cell; 
    } 
    elseif(indexPath.section == 1) 
    { 
    ... 
    } 
    else 
    { 
    ... 
    } 
} 

這些碼的目的是給當數據適合一行或多行時,將數據顯示在一行中調整電池高度並將數據顯示爲多行電池。

還糾正我,如果我應該保持自動佈局打開或關閉,並應該在轉換時設置故事板中的任何參數。

如果有人可以建議更好/更快,更簡單的方法,請用例子來做。

回答

3

嘗試沒有NSAttributedString ......這對我的作品:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    NSManagedObject *obj = [results objectAtIndex:indexPath.row]; 
    NSString *text = [obj valueForKey:@"name"]; 

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil]; 


    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size; 


    CGFloat height = MAX(size.height, 45.0f); 


    return height ; 
} 




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

    static NSString *CellIdentifier = @"Cell"; 


    TVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[TVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 

    NSManagedObject *obj = [results objectAtIndex:indexPath.row]; 
    NSString *text = [obj valueForKey:@"name"]; 

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONT_SIZE], NSFontAttributeName, nil]; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size; 

    [cell.cellText setText:text]; 
    [cell.cellText setFrame:CGRectMake(CELL_CONTENT_MARGIN, 0.0, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 45.0f))]; 

    return cell; 
} 

在TVCell.m:

self.cellText = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [self.cellText setLineBreakMode:NSLineBreakByWordWrapping]; 
    [self.cellText setMinimumScaleFactor:FONT_SIZE]; 
    [self.cellText setNumberOfLines:0]; 
    [self.cellText setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 
    self.cellText.textColor = [UIColor colorWithRed:64/255.0 green:59/255.0 blue:59/255.0 alpha:1.0]; 
    [self.contentView addSubview:self.cellText]; 
+0

謝謝你讓我嘗試一下,怎麼樣 - (UITableViewCell的*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath – iSrini

相關問題