2012-03-23 40 views
0

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; 

} 
+3

Checkoutでこのリンクを試してください:-http://stackoverflow.com/questions/9796947/how-can-i-resize-uitableviewcell-for-how-much-をtext-that-i-have/9798654#9798654 – Gypsa

答えて

1

あなたのセルの高さを計算し、この方法で返す:高さを割り当てるによると、

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

は、文字列のサイズを計算探してみてください細胞のあなたの2つのラベルの高さは、次のデリゲート内の個々の細胞にそれぞれの値を代入し計算した後、次のコードを使用

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{ 
    } 
0

この回答

#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; 
} 
関連する問題