2012-07-13 2 views
2

문제점은 다음과 같습니다. cellForRowAtIndexPath에 가변 배열을 지정하여 각 배열 객체를 셀에 표시합니다. 지금까지 그렇게 좋았던 셀은 예상대로 표시됩니다. 이제 나는 (조건에 따라) UILabel 첫 번째 셀에 다른 가변 배열 객체가 두 번째 셀로 옮겨 지길 원합니다. 문제는 그 조건을 테스트 할 때, 사실, UILabel은 첫 번째 셀 에 첫 번째 개체이 표시됩니다. 그래서 사실, 두 요소가 같은 셀에 있습니다. 이것은 내가 기대하는 것이 아닙니다. 나는 (조건이 true 일 때) 모든 요소를 ​​이동하여 첫 번째 셀을 UIlabel으로두기 위해 두 번째 셀에서 표시되도록하려고합니다. 내 논리cellForRowAtIndexPath 데이터 소스와 관련된 문제

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


    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"any-cell"]; 


// Add and display the Cell  
     cell.tag = [indexPath row]; 
     NSLog(@"cell.tag= %i",cell.tag); 
     //test the condition, if it's ok, then add the label to the first cell 
     if ([self isNoScoreLabelDisplayed] && cell.tag==0) { 
     UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 50)]; 
     [lbl setBackgroundColor:[UIColor greenColor]]; 
     [cell addSubview:lbl]; 
     } 
    cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(


     // 
     if (indexPath.row < cellList.count) { 

      [cell addSubview:[cellList objectAtIndex:[indexPath row]]];//cellList is the mutable array from which i get all the elements to display in the cells 

     }else{ 

      [cell addSubview:nextButton]; 
     } 


     return cell; 
} 

내가 실종 무엇인가 : 내가 무엇을 기대 저를 포기하지 않습니다

내 관련 코드이 무엇입니까? 고맙습니다.

+0

까지 제가 헤더와 비슷한 것을 알고 싶습니다. 왜이 헤더 테이블을 사용하지 않을까요? – Pfitz

+0

안녕하세요, 귀하의 회신에 감사드립니다. 실제로 머리글은 다른 것을하고 있습니다. 내 게임의 이름을 표시하고 있습니다. 헤더를 사용할 방법이 없습니다.) – Malloc

+0

'셀이 생성되는 곳은 어디입니까? 해당 코드를 확장 할 수 있습니까? –

답변

0

레이블 텍스트를 컨테이너의 첫 번째 요소로 삽입하여이를 확인할 수 있습니다. 이렇게하면 인덱스 오프셋과 코드의 추가 복잡성을 줄일 수 있습니다.

예.

[cellList insertObject:@"Label name" atIndex:0]; 
if ([cell tag] == 0) { 
    // add required label 
} 
else { 
    // do whatever you do for your standard cells, getting them with [cellList objectAtIndex:[indexPath row]]; 
} 
+0

답장을 보내 주신다면 더 자세히 설명해 주시겠습니까? – Malloc

+0

몇 가지 코드를 추가했습니다. – Alexander

+0

'// 필요한 라벨 추가 '를 의미합니다. 레이블을'cell : addSubview'에 추가하는 것을 의미합니까? 이런 식으로 :'[cell addSubview : myLabel];'? – Malloc

1

"제목"셀에 대해 오프셋되어야 할 때 indexPath.row를 모델 인덱스와 직접 연결합니다.

if (indexPath.row && (indexPath.row - 1) < cellList.count) { 
      [cell addSubview:[cellList objectAtIndex:indexPath.row - 1]]; 
} else { 
+0

답장을 위해 고맙습니다. 변수 상태의'FALSE' 검사에 대해서는 걱정하지 마십시오. 이미 검사했고,'FALSE' 일 때, UILabel은 전혀 표시되지 않고 모두 잘됩니다. 내 문제는 여전히 변수의 'TRUE'상태에 있습니다. – Malloc

+0

감사합니다. 소음을 제거하기 위해 답을 편집했습니다. –

1

나는이 u를 위해 물건을 정리하며, u는 레이블과 객체 셀을받지 않습니다이 두 가지 세포 식별자의 LabelCell 다른 하나는 일반 ArrayCell 하나를 사용합니다.

또한 저는 실제로 u r의 작업을 알지 못합니다. 그러나 u 셀에 subView를 추가 할 때마다 보이지만 어디서나 제거하지는 않습니다. 셀이 재사용된다는 사실을 잊지 마라.

1

안녕하세요, 라벨 조건을 확인한 다음 mutable 배열에 같은 객체를 추가하는 것 같습니다. indexPath.row == 0 < cell.count 첫 번째 행 당신이 레이블하지만 아래의 코드를 표시해야하는 경우

cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(

그래서 동일하게 간단하게 설정 휴대 태그 위의 코드는 + 1 indexpath.row하는 (기억이 처음 indexPath.row == 0, 그래서도 레이블을 표시하는 경우) 같은 배열 객체를 추가하십시오 :-)

if (indexPath.row < cellList.count) 
관련 문제