2012-03-02 168 views
1

我有表格視圖。 我想動態增加行的高度到一定的大小。 我該怎麼做。 我知道它的一個愚蠢的問題,但仍然找不到解決方案,請幫助我。 我已經使用這個code.But它不工作:動態增加行的高度--iphone

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

UILabel *lblOutTime = [[UILabel alloc]init]; 
lblOutTime = [[UILabel alloc] initWithFrame:CGRectMake(2, 20, 200, 40)]; 

lblOutTime.text [email protected]"Outtime : "; 

lblOutTime.backgroundColor = [UIColor clearColor]; 
lblOutTime.font = [UIFont boldSystemFontOfSize:5.0f]; 
lblOutTime.font = [UIFont fontWithName:@"HelveticaNeue Heavy" size:5.0f]; 

[cell.contentView insertSubview:lblOutTime atIndex:1]; 
    return cell; 

}

+0

這裏我試圖增加標籤高度。我將標籤放在行(單元格)上。 – 2012-03-02 12:39:37

+1

In heightForRowatIndexPath根據你的內容計算高度併爲每個單元格返回適當的值 – 2012-03-02 12:41:10

+0

我的內容是一些字符串值。如果我將傳遞一些靜態高度,那麼將會是確定的。但是我該怎麼做呢?我的知識非常少,因爲我是iPhone的新手。 – 2012-03-02 12:43:38

回答

1

-

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if (indexPath.row == myIndexPath) 
     { 
      return someHeight; 
     } 
     return normalHeight; 
    } 
In this case the height is not dynamic. here 

只有2固定單元格高度的位置。如果條件顯示爲 。

+0

想知道如何增加動態高度? – 2012-04-27 06:22:58

1

在這裏,您可以更改單元格的大小:

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

if (indexPath.myRow == myCellIndex) 
return NEW_HEIGHT; 
else return NORMAL_HEIGHT; 

} 

而且你可以重新載入你的表,只要你想要:

[myTable reloadData]; 
+0

我該如何聲明myrow和mycellindex?無法得到。我正在猜測一些字符串數組並在tableview單元格中顯示它們。對不起,如果我問過有趣的問題 – 2012-03-02 12:48:08

+0

indexPath.myRow是單元格被按下的行,而myCellIndex是你創建的變量,其中變量yu的索引要增加。 – 2012-03-02 12:50:25

+1

感謝所有。它正在工作 – 2012-03-02 12:59:30

0

如果你想增加所有行使用的高度

[tableView setRowHeight:44]; 

[tableView reloadData]; 

如果你想增加一個特定的行使用的UITableView Delegete heightForRowAtIndexPath:

的高度,並使用該

[tableView reloadData]; 
0

此的tableview委託方法添加到您的class.m文件。 確保你的類符合UITtableViewDelegate協議

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     if (indexPath.row == myIndexPath) 
     { 
      return someHeight; 
     } 
     return normalHeight; 
    } 
+0

感謝所有。它正在工作 – 2012-03-02 13:00:05

1
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row <[ListOfCartItems count]) 
    { 
     CGFloat  result = 44.0f; 
     NSString* text = nil; 
     CGFloat  width =220 ; 

     clsCart *tempCartPro; 
     tempCartPro=[ListOfCartItems objectAtIndex:indexPath.row]; 

     text=tempCartPro.clsProductItem.productName; 

     if (text) 
     { 

      CGSize  textSize = { width, 20000.0f };  // width and height of text area 
      CGSize  size = [text sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap]; 

      size.height += 90.0f;   // top and bottom margin 
      result = MAX(size.height, result) ; // at least one row 
     } 

     return result; 

    } 
    else 
    { 
     return 230.0; 
    } 
}