如何根据表格单元格内容增加UITableviewcell高度运行时间?

时间:2014-04-05 06:45:05

标签: iphone objective-c uitableview ios7

你好我有默认单元格高度44.0的UITable视图,现在我们想根据单元格内容增加它,如图像enter image description here

我正在使用此功能,但这不起作用.....

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   int rowHeight =0.0f;
//  NSString *stringToSize = [shared.combine_address objectAtIndex:indexPath.row];
 CGSize size = [ [shared.combine_address objectAtIndex:indexPath.row] sizeWithFont:[UIFont    systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(743, 44) lineBreakMode:NSLineBreakByWordWrapping];
rowHeight = self.table.rowHeight+size.height;
return rowHeight;
}


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

static NSString *MyIdentifier = @"MyIdentifier";

//   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

SWTableViewCell *cell2 = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];


if(tableView==table)
{
    //static NSString *cellIdentifier = @"Cell";

    if (cell2 == nil) {

        cell2 = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:MyIdentifier
                                   containingTableView:table // Used for row height and selection
                                    leftUtilityButtons:nil
                                   rightUtilityButtons:[self rightButtons]];
        cell2.delegate = self;
        cell2.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    NSLog(@"CELL 1 %@",cell2.textLabel.text);
    cell2.textLabel.lineBreakMode=NSLineBreakByWordWrapping;
    cell2.textLabel.numberOfLines=0;
    cell2.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
    cell2.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell2.detailTextLabel.numberOfLines = 0;
    [cell2.textLabel sizeToFit];

}
return cell2;

 }

3 个答案:

答案 0 :(得分:0)

宽度错误或shared.combineAddress没有值。

尝试将宽度设为320

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   int rowHeight =0.0f;
   NSString *stringToSize = [shared.combine_address objectAtIndex:indexPath.row];
   CGSize size = [stringToSize sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(320, 44) lineBreakMode:NSLineBreakByWordWrapping];
   rowHeight = self.table.rowHeight+size.height;
   return rowHeight;
}

还要检查stringToSize是否有一些值。

编辑:

    UILabel *sizeCalculationLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 0)];
    sizeCalculationLabel.numberOfLines = 0;
    [sizeCalculationLabel setFont:[UIFont systemFontOfSize:13.0f]];
    sizeCalculationLabel.text = stringToSize;
    [sizeCalculationLabel sizeToFit];
    return sizeCalculationLabel.frame.size.height;

答案 1 :(得分:0)

我认为你正在使用iPad。如果您的UITableView的宽度超过743,则可以更改以下内容。

constrainedToSize:CGSizeMake(743, 44)值更改为constrainedToSize:CGSizeMake(743, 999)

确保将宽度max作为tableView的宽度传递。因此,如果你的UITableView的高度是320(iPhone的标准)而不是使用constrainedToSize:CGSizeMake(320, 999)

当您通过44高度的约束尺寸时,它表示最大高度应为44.因此,您必须传递您想要显示的最大尺寸可能是100,200或任何数字。我将它设置为999。

在iOS 7中也不推荐使用sizeWithFont。因此最好使用

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

iOS 7中的

以下是示例:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
   int rowHeight =0.0f;
   CGSize size;
   if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont    systemFontOfSize:13.0f] forKey: NSFontAttributeName]; 
        size = [[shared.combine_address objectAtIndex:indexPath.row] boundingRectWithSize:CGSizeMake(743, 999) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin  attributes:stringAttributes context:nil].size;

   }
   else {
       size = [ [shared.combine_address objectAtIndex:indexPath.row] sizeWithFont:[UIFont    systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(743, 999) lineBreakMode:NSLineBreakByWordWrapping];
   }
   rowHeight = self.table.rowHeight+size.height;
   return rowHeight;
}

答案 2 :(得分:0)

表格视图单元格使用动态高度UITableViewAutomaticDimension

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

 return UITableViewAutomaticDimension;
 }

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

 return 44.0f;

 }