2014-04-16 2 views
2

cellForRowAtIndexPath 대리자 방식으로 반환하기 위해 셀을 초기화하려면 어떻게해야합니까?cellForRowAtIndexPath에서 두 개의 다른 셀 반환

var i가 있다고 가정합니다. i는 0 또는 1이 될 수 있습니다. 는 : 그래서

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
id cell=nil; 
switch (i) { 
    case 0:{ 
     static NSString *[email protected]"RecommendTableCellIdentifier"; 
     RecommendTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell==nil) { 
      cell=[[RecommendTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     dataRow=[dataList objectAtIndex:[indexPath row]]; 
     [cell addContent:dataRow]; 
     [cell.wantToSeeButton setTag:[indexPath row]]; 
     [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
     break; 
    case 1:{ 
     static NSString *[email protected]"WantTableCellIdentifier"; 
     WantTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell==nil) { 
      cell=[[WantTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     dataRow=[dataList objectAtIndex:[indexPath row]]; 
     [cell addContent:dataRow]; 
     [cell.wantToSeeButton setTag:[indexPath row]]; 
     [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
     break; 

    default: 
     break; 
} 
return cell; 

} 

모두 CustomCellA 및 B는 클래스 XIB로 정의된다.

어떻게해야합니까? 감사합니다.

+0

'break; 앞에 사용자 셀을'id 셀 '에 할당하십시오. ie 0 경우 셀 = recommendTableCell이고 1이면 cell = wantTableCell입니다. – Akhilrajtr

+1

또는 각 if if 레그에서'cell'을 리턴합니다. 문제는 거기에 정의 된 세 가지 다른 버전의 '셀'을 가지고 있으며 두 가지를 설정했지만 세 번째 버전을 반환한다는 것입니다. –

답변

4

는이 작업을 수행 :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
id cellToReturn=nil; 
switch (i) { 
    case 0:{ 
     static NSString *[email protected]"RecommendTableCellIdentifier"; 
     RecommendTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell==nil) { 
      cell=[[RecommendTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     dataRow=[dataList objectAtIndex:[indexPath row]]; 
     [cell addContent:dataRow]; 
     [cell.wantToSeeButton setTag:[indexPath row]]; 
     [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
     cellToReturn=cell; 
     break; 
    case 1:{ 
     static NSString *[email protected]"WantTableCellIdentifier"; 
     WantTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell==nil) { 
      cell=[[WantTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     dataRow=[dataList objectAtIndex:[indexPath row]]; 
     [cell addContent:dataRow]; 
     [cell.wantToSeeButton setTag:[indexPath row]]; 
     [cell.wantToSeeButton addTarget:self action:@selector(iWantToSeeClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
     cellToReturn=cell; 
     break; 

    default: 
     break; 
} 

return cellToReturn; 

} 

희망이 도움이 ... :)

+0

도움이되었습니다. 이런 종류의 문제를 해결하는 더 좋은 방법이 있습니까? 두 경우 모두 대부분의 코드가 동일합니다. 다른 더 좋은 방법이있을 수 있습니까? – user3527021

+0

죄송합니다. 네가 포스터를 쓸 때 내 나라에서 밤이었다. 늦어서 죄송합니다. 나는 당신이이 두 세포의 목적이 무엇인지 그리고 그것들의 차이점이 무엇인지 말하면 이것을 할 수있는 더 좋은 방법을 제안 할 것입니다. – Rashad

+0

그 좋은 방법이 될 수 있습니다 : 나는 여러 세그먼트를 가지고보기, 내가 세그먼트를 선택할 때마다 다른 셀을 가진 다른 테이블 뷰는 내가 클릭 한 세그먼트에 따라 나타날 것입니다. 이 문제를 해결하는 더 좋은 방법에 대해 조언을 해줄 수 있습니까? 덕분에 ! – user3527021

0

나의 이해는 당신이 코드 repitition을 피하려고합니까? 이것을 시도하십시오 :

+0

고마워요! 그러나 그것은 몇 가지 실수를 돌 렸습니다! – user3527021

+0

무엇을 의미합니까? 어떤 오류가 발생하면 – meda

+0

예. 'wantToSeeButton'속성이 '__strong id'유형의 객체에 없습니다. wantToSeeButton은 RecommendTableCell 및 WantTableCell에서 모두 선언 된 IBOutlet입니다. – user3527021

관련 문제