2011-10-24 11 views
0

私はiPhoneアプリの新しいプログラマだ...私は7つのラベルとテーブルビューの最初のセルで1つのImageViewのを持っている......複数のラベル... iphone

私はこのコードを書きますこの仕事は満足です...(スクロールするときに時間がかかるかもしれません)

教えてください...これはこの仕事をするかしないのですか? ...私に..the正しい方法を教えてくださいない場合は、電池を再利用している場合

...事前に

おかげ

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]]; 

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease]; 

} 


if(indexPath.row==0) 
{ 


      CGRect frame=CGRectMake(120,10, 80, 40); 

      UILabel *label1=[[UILabel alloc]init]; 

      label1.frame=frame; 

    [email protected]"first label"; 

    [cell.contentView addSubview:label1]; 

      [label1 release]; 




    CGRect frame2=CGRectMake(200,10, 80, 40); 

      UILabel *label2=[[UILabel alloc]init]; 

      label2.frame=frame2; 

    [email protected]"second label"; 

    [cell.contentView addSubview:label2]; 

      [label2 release]; 


    and so on....... 


} 
    else if(indexPath.row==1) 
    { 
     //add four labels for this cell here...... 
    } 


return cell; 
} 

答えて

3

あなたはラベルを作成するために、第2の時間を必要としません。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease]; 

     if(indexPath.row==0) 
     { 
      CGRect frame=CGRectMake(120,10, 80, 40); 
      UILabel *label1=[[UILabel alloc]init];    
      label1.frame=frame; 
      [email protected]"first label"; 
      label1.tag = 1001; 
      [cell.contentView addSubview:label1]; 
      [label1 release]; 

      CGRect frame2=CGRectMake(200,10, 80, 40); 
      UILabel *label2=[[UILabel alloc]init]; 
      label2.frame=frame2; 
      [email protected]"second label"; 
      label2.tag = 1002; 
      [cell.contentView addSubview:label2]; 
      [label2 release]; 

      and so on....... 
     } 
    } 


    if(indexPath.row==0) 
    { 
     UILabel *label1=[cell viewWithTag:1001]; 
     [email protected]"first label"; 

     UILabel *label2=[cell viewWithTag:1002]; 
     [email protected]"second label"; 

     and so on....... 
    } 


    return cell; 
} 

私は以前に作成したラベルにtag値を使用してアクセスしています。

+0

@Nekto ...提案ありがとう....私は私のラベルをどこで定義するのか聞いてみたい...(.hファイル内)...そこには4つの異なるラベルがあります私の2番目のテーブルのセル.... – GauravBoss

+0

...これを行うための正しい方法をお勧めすることができます...事前にお礼 – GauravBoss

+0

私はあなたの問題を解決する必要がありますコードを掲載しました – Nekto

0

私は、InterfaceBuilderでこのような複雑なセルを設計する方がずっと簡単だと思います。ストーリーボードを使用している場合、テーブルビューですぐにカスタムセルを設計できます。 xibsを使用している場合は、テーブルビューの代わりにカスタムUITableViewCell、所有者としてUIViewController、プロジェクトのUITableViewCellのサブクラスを宣言するペン先を作成できます。それはあなたの苦しみをもっと簡単にするはずです=)