【问题标题】:Table view cell expand or contract with the size of the text表格视图单元格随文本大小展开或收缩
【发布时间】:2012-03-23 05:08:38
【问题描述】:

我一直在用从数据库中获取的数据填充 UITable 视图单元格。不同单元格的数据长度不同。所以我需要扩展或收缩高度取决于数据长度。目前我正在使用带有以下代码的自定义单元格

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

    static NSString *cellID= @"customcell4fan";
    customcell4fan *cell = (customcell4fan *)[tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell==nil)
    {
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"customcell4fan" owner:nil options:nil];

        for(id currentObject in nibObjects)
        {
            if([currentObject isKindOfClass: [customcell4fan class]])
            {
                cell = (customcell4fan *)currentObject;
            }

        }
    }

eleme = [xmlElementObjects objectAtIndex:indexPath.section];

NSString *email= [eleme.title stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
    NSString *postData= [eleme.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
cell.nameLabel.text=email;
    cell.postLabel.text=postData;

  return cell;

}

【问题讨论】:

标签: iphone objective-c ios uitableview resizable


【解决方案1】:
【解决方案2】:

计算单元格的高度并在此方法中返回:

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

【讨论】:

    【解决方案3】:

    计算字符串的大小,根据单元格的高度来分配。

    要计算标签的高度,请使用以下代码,

    CGSize stringSize = [urResponseStr sizeWithFont:[UIFont boldSystemFontOfSize:15]
                          constrainedToSize:CGSizeMake(320, 9999)
                              lineBreakMode:UILineBreakModeWordWrap];//stringSize.height is your label height
    

    在计算两个标签的高度后,将各自的值分配给以下委托中的各个单元格。

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

    【讨论】:

      【解决方案4】:

      试试这个答案

      #define FONT_SIZE 14.0f
      #define CELL_CONTENT_WIDTH 320.0f
      #define CELL_CONTENT_MARGIN 10.0f
      
      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
      {
        NSString *text = [items objectAtIndex:[indexPath row]];
      
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
      
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
      
        CGFloat height = MAX(size.height, 44.0f);
      
        return height + (CELL_CONTENT_MARGIN * 2);
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
        UITableViewCell *cell;
        UILabel *label = nil;
      
        cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil)
        {
          cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
      
          label = [[UILabel alloc] initWithFrame:CGRectZero];
          [label setLineBreakMode:UILineBreakModeWordWrap];
          [label setMinimumFontSize:FONT_SIZE];
          [label setNumberOfLines:0];
          [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
          [label setTag:1];
      
          [[label layer] setBorderWidth:2.0f];
      
          [[cell contentView] addSubview:label];
      
        }
        NSString *text = [items objectAtIndex:[indexPath row]];
      
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
      
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
      
        if (!label)
          label = (UILabel*)[cell viewWithTag:1];
      
        [label setText:text];
        [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
      
        return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-21
        相关资源
        最近更新 更多