2011-10-24 5 views
0

저는 아이폰 앱의 새로운 프로그래머입니다 ... 테이블보기의 첫 번째 셀에 7 개의 라벨과 1 개의 이미지 뷰가 있습니다 ......테이블 셀에 여러 라벨 ... iphone

이 코드는 그 .....이 작품은 만족 스럽습니다 ... (스크롤 할 때가 걸릴지도 모릅니다)

제발 말해주세요 ...이 일을 할 것인가 아닌가 .... ....? ... 나에게 용맹 한 올바른 방법을 알려 주시기 바랍니다없는 경우 셀을 재사용하는 경우

... 사전에

감사

- (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

당신은 레이블을 만드는 두 번째 시간이 필요하지 않습니다

- (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]; 
     label2[email protected]"second label"; 

     and so on....... 
    } 


    return cell; 
} 

나는 tag 값을 사용하여 이전에 생성 된 레이블에 액세스하고 있습니다.

+0

@Nekto ... 제안 주셔서 감사합니다 .... 나는 내 레이블을 정의하는 곳을 물어보고 싶습니다 ... (.h 파일) ... 네 가지 다른 레이블이 있습니다. 내 두 번째 테이블 셀 .... – GauravBoss

+0

...이 일을위한 올바른 방법을 제안 할 수 ... 사전에 덕분에 – GauravBoss

+0

귀하의 문제를 해결해야합니다 코드를 게시했습니다 – Nekto

0

저는 InterfaceBuilder에서 복잡한 셀을 디자인하는 것이 훨씬 쉬울 것이라고 생각합니다. 스토리 보드를 사용하는 경우 테이블보기에서 맞춤형 셀을 바로 디자인 할 수 있습니다. xibs를 사용하는 경우 테이블 뷰 대신 사용자 정의 UITableViewCell, 소유자로 UIViewController, 프로젝트에서 UITableViewCell의 하위 클래스를 선언하는 펜촉을 만들 수 있습니다. 그게 당신의 고통을 훨씬 쉽게 만들어야합니다 =)

관련 문제