2011-08-09 73 views
1

您好我是iPad新手。所以我使用web服務在tableview中顯示數據。那麼我怎麼能改變我嘗試過的每個單元格的行高,但是它顯示出了更高的高度呢? 我用三個數組在tableview中顯示數據所以,如果我通過web服務給數組更多的數據。那麼我怎樣才能改變表格視圖的每一行。誰能幫我 ?如何在tableview中動態更改ipad行的高度

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [arrOutline1 count] + [arrOutline2 count];   
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if ((indexPath.row)%2==0) { 
     return 100;   
    } 
    else {   
     //strData3 =[[NSMutableString alloc]init]; 
     NSLog(@" the data is %@",[arrOutline3 objectAtIndex:countRow]); 

     //strData3 = [NSMutableString stringWithFormat:@"%@",[arrOutline3 objectAtIndex:countRow]]; 
     int myLength = [[NSMutableString stringWithFormat:@"%@",[arrOutline3 objectAtIndex:countRow]] length]; 
     NSLog(@" the length of dat is :%d",myLength); 

     int LengthCell; 
      int LengthCell = myLength /3; 
      CGFloat heightCell3 ; 
     int l = LengthCell * 19; 
     heightCell3 =(CGFloat)l; 
     NSLog(@" the heigh of cell is :%f",heightCell3); 
     countRow = countRow + 1; 
     return heightCell3; 
    } 
} 

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

     static NSString *CellIdentifier = @"Cell"; 

     if ((indexPath.row)%2==0) { 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

      if (cell == nil) { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
      } 

      txtlable1= [[UILabel alloc]initWithFrame:CGRectMake(50, 0, W, H)]; 
      txtlable1.backgroundColor=[UIColor clearColor]; 
      txtlable1.font=[UIFont fontWithName:@"Arial" size:25.0]; 
      txtlable1.text =[arrOutline1 objectAtIndex:count2]; 
      txtlable1.lineBreakMode = UILineBreakModeWordWrap; 
      txtlable1.textAlignment = UITextAlignmentLeft; 
      txtlable1.numberOfLines = 0; 
      [[cell contentView]addSubview:txtlable1]; 
      [txtlable1 release];    

     txtlable2= [[UILabel alloc]initWithFrame:CGRectMake(600, 0, W-200, H)]; 
      txtlable2.backgroundColor=[UIColor clearColor]; 
      txtlable2.font=[UIFont fontWithName:@"Arial" size:20.0]; 
      txtlable2.textAlignment = UITextAlignmentLeft; 
      txtlable2.lineBreakMode = UILineBreakModeWordWrap; 
      txtlable2.numberOfLines = 0; 
      txtlable2.text =[arrOutline2 objectAtIndex:count2]; 
      [[cell contentView]addSubview:txtlable2]; 
      count2 =count2+1; 
      [txtlable2 release]; 
      return cell; 

     } 
    else { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     int myLength = [[NSMutableString stringWithFormat:@"%@",[arrOutline3 objectAtIndex:count1]] length]; 
     NSLog(@" the length of dat is :%d",myLength); 
     int LengthCell = myLength /3; 
     CGFloat heightCell3 ; 
     int l = LengthCell * 15; 
     heightCell3 =(CGFloat)l; 
     NSLog(@" the heigh of cell is :%f",heightCell3); 

     txtlable3=[[UILabel alloc]initWithFrame:CGRectMake(50,0,W+400,heightCell3)]; 
     txtlable3.backgroundColor=[UIColor clearColor]; 

     txtlable3.font=[UIFont fontWithName:@"Arial" size:30.0]; 
     txtlable3.lineBreakMode = UILineBreakModeWordWrap; 
     txtlable3.numberOfLines = 0; 

     txtlable3.text =[arrOutline3 objectAtIndex:count1]; 
     [cell addSubview:txtlable3]; 
     count1 = count1 + 1; 

     return cell; 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    arrOutline1 = [[NSMutableArray alloc]initWithObjects:@"vijay sankar ramamknkakjdkl.\n the thinking the main. candidate of things thisisingamaldaj",@"Amith",@"Gupta",@"RightData",@"RightData1",@"RightData2",@"RightData3",@"RightData4",@"RightData5",@"RightData6",nil]; 
    arrOutline2 = [[NSMutableArray alloc]initWithObjects:@"ganesh",@"surya",@"sudha",@"LeftData",@"LeftData1",@"LeftData2",@"LeftData3",@"LeftData4",@"LeftData5",@"LeftData6",nil]; 
    arrOutline3 = [[NSMutableArray alloc]initWithObjects:@"vijay sankar ramamknkakjdkl.\n the thinking the main.\n candidate of things thisisingamaldaj .\n ",@"jagadish",@"night fhdfhda fhadfhadf.\n dfafhdjkahfa",@"3rdData",@"3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.\n candidate of things thisisingamaldaj",@"3rdData2",@"3rdData3",@"3rdData4",@"3rdData5",@"3rdData6 3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.",nil]; 

    CGFloat heihtTable; 
    heihtTable =[arrOutline1 count] + [arrOutline3 count] ; 
    heihtTable =heihtTable *175 ; 
    scrl.contentSize=CGSizeMake(728,heihtTable); 
    count1=0; 
    count2=0; 
    countRow =0; 
     lblCompetency = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 400, 100)]; 
    lblCompetency.text = @"Competency"; 
    lblCompetency.font = [UIFont fontWithName:@"Helvetica" size: 24.0]; 
    [self.scrl addSubview:lblCompetency]; 

    NSLog(@"The height is %f",heihtTable); 
    TableOutline = [[UITableView alloc] initWithFrame:CGRectMake(10, 150,1000,heihtTable) style:UITableViewStyleGrouped]; 
    TableOutline.separatorColor=[UIColor blackColor]; 
    TableOutline.backgroundColor=[UIColor clearColor]; 
    [TableOutline setUserInteractionEnabled:NO]; 
    TableOutline.opaque=YES; 
    TableOutline.delegate =self; 
    TableOutline.dataSource = self; 
    TableOutline.allowsSelection=FALSE; 

    UIView *viewTable2=[[UIView alloc]initWithFrame:CGRectMake(10, 200, 1000, heihtTable)]; 
    viewTable2.backgroundColor=[UIColor clearColor]; 
    TableOutline.backgroundView =viewTable2; 
    [self.scrl addSubview:TableOutline];  
} 

回答

1
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath 
{ 
    CGSize labelSize = CGSizeMake(200.0, 20.0); 
    NSString *strTemp = [arrServices objectAtIndex:indexPath.row]; 
    if ([strTemp length] > 0) 
     labelSize = [strTemp sizeWithFont: [UIFont boldSystemFontOfSize: 14.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap]; 
    return (labelSize.height + 10); 
} 

這是我在我的項目中使用,它工作得很好。根據您的要求更改值。

2

你或許可以嘗試委託方法:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath 

,並指定基於indexpath電池參數的高度。

+0

k謝謝,但我需要根據數據長度來決定每個單元格的高度。 –

+0

@Praveen這不是要更改高度的地方 –

+0

@Srikar:討論 表視圖在它使用單元繪製一行之前將其發送給它的委託,從而允許委託在顯示之前自定義單元對象。此方法爲委託人提供了覆蓋表視圖較早設置的基於狀態的屬性的機會,例如選擇和背景顏色。委託返回後,表視圖僅設置alpha和frame屬性,然後僅在滑動或滑出行時對行進行動畫處理。 –

2

您可以在此委託更改UITableViewCell高度 -

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

對於這個工作,你可以動態地計算出的內容高度上飛還是有一些預定義的高度100150200等&基於一些截止上你的數據,使用其中的一個高度...將減少複雜性。

否則,你可能希望找到長度