2012-06-06 3 views
1

안녕하세요, 저는 스토리 보드에 4 개의 프로토 타입 셀을 사용합니다. 각 프로토 타입 셀에 대해 나는 내 레이블로 클래스 uitableviewCell을가집니다.프로토 타입 세포 uitableview

내 코드를 보면 :

섹션 0 섹션 1, 내가이가 내 레이블하지만, 섹션 2의 경우 0에서 datas가에서
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    static NSString *CellIdentifier = @"ProfilCell"; 
    static NSString *CellIdentifier1 = @"ProfilCell1"; 
    static NSString *CellIdentifier2 = @"ProfilCell2"; 
    static NSString *CellIdentifier3 = @"ProfilCell3"; 
    static NSString *CellIdentifier4 = @"ProfilCell4"; 

    if(indexPath.section == 0) 
    { 
     ProfilCell* cell =(ProfilCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     cell.pseudoLabel.text=[[infoArray objectAtIndex:indexPath.row]valueForKey:@"pseudo"]; 

     return cell; 

    } 

     if(indexPath.section == 1) 
     { 
     ProfilCell2* cell = (ProfilCell2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

     cell.textLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"status"]; 

     return cell; 

    } 

    if(indexPath.section == 2) 
    { 
     switch (indexPath.row) 
     { 
      case 0: 
      { 

       ProfilCell3* cell =(ProfilCell3*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
       cell.ageLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"age"]; 
       return cell; 
      } 
       break; 

      case 1: 

      { 

       ProfilCell4* cell = (ProfilCell4*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier3]; 
       cell.deptLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"localite"]; 

       return cell; 

      } 
       break; 
      case 2: 
      { 
       ProfilCell5* cell = (ProfilCell5*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier4]; 


       cell.membreLab.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"membre"]; 

       return cell; 

      } 
       break; 
      default: 
       break; 
     } 
    } 
      return nil; 
} 

: 내 배열이 나타납니다 왜

[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 

이 될 수를 비어있는

이제 배열에 객체가 하나뿐이므로 indexPath에 0을 주면됩니다.

답변

1

당신은 아마 이런 식으로 뭔가를 시도해야 ...

내가 여기했던 것이 무엇이든 할 수 있다는 것을 의미, 종류 id의 셀에 대한 VAR로 설정되어 무엇
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"ProfilCell"; 
    static NSString *CellIdentifier1 = @"ProfilCell1"; 
    static NSString *CellIdentifier2 = @"ProfilCell2"; 
    static NSString *CellIdentifier3 = @"ProfilCell3"; 

    id cell = nil; 

    if(indexPath.section == 0) { 
       cell = (ProfilCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
       cell.pseudoLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"pseudo"]; 
    } 

    if(indexPath.section == 1) { 
      cell = (ProfilCell2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
      cell.statusLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"status"]; 
    } 

    if(indexPath.section == 2) { 
      cell =(ProfilCell3*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
      cell.ageLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"age"]; 
    } 

    if(indexPath.section == 3) { 
      cell = (ProfilCell4*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier3]; 
      cell.deptLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"localite"]; 
    } 


    return cell; 

    } 

. if 문에서 셀이 무엇인지 결정하고 이에 따라 셀을 설정합니다. 끝에있는 셀의 인스턴스를 1 개만 반환하며 각 문에는 반환하지 않습니다. 이 dequeueReusableCellWithIdentifier: 당신이 nil에 대한 반환 값을 확인해야 호출 한 후

+0

'if'문을 사용하지 않으면 'return cell'이 nil을 반환합니다. 원래 코드 게시와 다른 점은 무엇입니까? – GoZoner

+1

하드 코딩 된 행이 아니고 더 많거나 적지 않은지 어떻게 알 수 있습니까? ... – skram

-2

도움이되기를 바랍니다. 반환 값이 nil이 아니면 괜찮습니다. 큐에서 재사용 가능한 셀을 가져 왔습니다. 그러나 반환 된 값이 nil이면 큐에서 사용할 수있는 재사용 가능한 셀이 없습니다.

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:@"TableViewCell"] autorelease]; 

또는 .xib 파일에서로드하여 : (.이 변함없이 새로 만든 테이블의 경우)이 후자의 경우에, 당신도 프로그래밍 방식 allocinit를 사용하여, 새로운 셀을해야합니다 : 귀하의 경우에는

cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" 
             owner:self options:nil] lastObject]; 

, 당신은 (다른 섹션에 대한 반복) 다음과 같은 무언가를 시도 할 수 있습니다 :

if (indexPath.section == 0) 
{ 
    ProfilCell *cell = (ProfilCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) // if we did not get a reusable cell from the queue, we make one… 
    { 
     cell = [[ProfilCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.pseudoLabel.text= [[infoArray objectAtIndex:indexPath.row]valueForKey:@"pseudo"]; 
    return cell; 
} 
+1

안녕하세요, 귀하의 방법을 사용했지만 또한이 오류와 충돌했습니다 : *** 캐치되지 않은 예외 'NSInvalidArgumentException'이유로 인한 응용 프로그램 종료 : '- [NSIndexPath setTableViewStyle :] : 인식 할 수없는 선택자를 인스턴스로 보냄 – XcodeMania

+5

스토리 보드를 사용할 때 prototype 셀의 경우 dequeueReusableCellWithIdentifier는 항상 유효한 셀을 반환합니다. 명시 적으로 할당/초기화 할 필요는 없습니다. – GoZoner

+1

당신이 맞아요. 내 코드에서 alloc을 사용하지 않았습니다. – XcodeMania

1

당신은 그 개를 확인해야합니다 dequeue에 대한 통화 요청 중 ... 재사용 가능 ... 실제로 셀을 반환 중입니다. 사용 : 각각의 'if'문 본문에

NSAssert (cell, @"Missed Cell"); 

본문에 사용하십시오. 내 의심은 무효 셀이 반환된다는 것입니다. 이것은 "ProfilCell", "ProfilCell1"...의 재사용 식별자가 스토리 보드의 셀 식별자와 일치하지 않기 때문입니다. 이를 해결하려면 스토리 보드로 이동하여 각 프로토 타입 셀을 하나씩 선택하고 '식별자'가 코드에있는 것과 일치하는지 확인하십시오. 그렇지 않은 경우 동의로 변경하십시오.

'식별자'가 일치해야합니다. '라벨'이 아니야! ProfilCell, ProfilCell2, ProfilCell3 및 ProfilCell4이

있는 UITableViewCell

의 모든 서브 클래스입니다

+0

이제 괜찮습니다. – XcodeMania

0

확인 또한 휴대 식별자는 스토리 보드에 일치하는지 확인합니다.