2011-08-09 4 views
1

salut je suis nouveau sur iPad en développement. Donc j'utilise le service web pour afficher les données dans tableview. Alors, comment puis-je changer la hauteur de ligne de chaque cellule que j'ai essayé, mais elle montre plus de hauteur pour celle-là? J'ai utilisé trois tableaux pour afficher des données dans tableview Donc, si je donne plus de données à la matrice via le service Web. Alors, comment puis-je changer pour chaque ligne pour la vue de la table. Quelqu'un peut-il m'aider ?comment changer la hauteur de ligne dans tableview ipad dynamiquement

- (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];  
} 

Répondre

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); 
} 

C'est ce que je dans mon projet et cela a fonctionné parfaitement. Changez les valeurs selon vos besoins.

2

Vous pouvez probablement essayer la méthode déléguée:

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

et spécifier la hauteur du paramètre de cellule sur la base indexPath.

+0

k Merci, mais je dois décider de la hauteur de chaque cellule en fonction de la longueur des données. –

+0

@Praveen ce n'est pas l'endroit pour changer la hauteur –

+0

@Srikar: Discussion Une vue de table envoie ce message à son délégué juste avant qu'il n'utilise une cellule pour dessiner une ligne, permettant ainsi au délégué de personnaliser l'objet de cellule avant qu'il ne soit affiché. Cette méthode permet au délégué de remplacer les propriétés basées sur l'état définies précédemment par la vue de la table, telles que la couleur de sélection et la couleur d'arrière-plan. Une fois le délégué renvoyé, la vue de table définit uniquement les propriétés alpha et frame, puis uniquement lors de l'animation des lignes lorsqu'elles sont déplacées ou déplacées. –

2

Vous pouvez modifier UITableViewCell la hauteur dans ce délégué -

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

Pour que cela fonctionne, vous pouvez dynamiquement déterminer la hauteur du contenu à la volée ou ont des hauteurs prédéfinies 100150200 etc. & basé sur une coupure sur vos données, utilisez une de ces hauteurs ... Cela réduira la complexité.

Sinon, vous pouvez trouver la longueur


Questions connexes