2011-12-02 2 views
5

이 내 cellForRowAtIndexPath jQuery과 위임 방법에 대한 요약 된 코드이다 :jQuery과 indexPath 부 논리

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
cell.textLabel.text = @""; 
cell.detailTextLabel.text = @""; 
cell.backgroundView = NULL; //problem here 

// Initialize cell 
//blah blah blah 
//now to the good part... 

if(indexPath.section == 1) { 
    cell.backgroundView = deleteButton; 
    cell.userInteractionEnabled = YES; 
    cell.textLabel.text = nil; 
    cell.detailTextLabel.text = nil; 
} 

else if(indexPath.section == 0) { 
    NSLog(@"section: %i row: %i", indexPath.section, indexPath.row); 
    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text = @"foobar"; 
      //more stuff 
      break; 

     //lots more cases 

     default: 
      break; 
    } 
} 

return cell; 

}

내 문제 부 (1) (제 0의 첫번째 셀 10 셀 부 (1)를 갖는다는 것이다 단 1 셀만 있음)에는 첫 번째 섹션의 셀 0에 할당되는 것으로 가정되는 정보가 할당됩니다. 따라서, deleteButton background와 etc를 얻지 않고, 레이블 제목 "foobar"를 얻습니다. 내 if 문이 매우 명확하기 때문에 왜 이런 일이 발생하는지 잘 모르겠습니다. 어떤 아이디어?

편집 : backgroundView를 NULL로 설정하면 텍스트가있는 셀이보기를 벗어날 때 배경없이 되돌아옵니다. 따라서 이것이 가능한 솔루션이 아닙니다. 또한 detailTextLabel의 텍스트는 텍스트가 없어야하는 셀에 설정됩니다.

이가 전무로 설정 셀 backgroundViews 및 삭제 셀을 보여주는 텍스트로 모양을 어디는 안 :

enter image description here

솔루션, 알렉스 간주 할 (권장)이 코드와 함께 오래 디큐 코드를 교체 :

NSString* identifier; 
if(indexPath.section == 0) 
    identifier = @"0"; 
else 
    identifier = @"1"; 
UISwitch *switchView; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease]; 

답변

5

셀 재사용에 관한 설명서를 읽어야합니다.

기본적으로 다르게 스타일이 지정된 셀이므로 두 섹션 각각에 다른 reuseIdentifier를 사용해야합니다.

+0

아, 그래, 그랬어. 해결책으로 질문을 업데이트했습니다. –

0

이 코드 줄에 닫는 } 브래킷을 추가

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) { 
    // No cell to reuse => create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
.... 
.... 
.... 
if(indexPath.section == 1) { 
    .... 
    .... 
} 
if(indexPath.section == 0) { 
    .... 
    .... 
} 

그리고 나는 당신이 당신의 테이블에서 더 나은 결과를 낼 것이라고 생각합니다.

현재 상황이 올바르게 작동하고 있습니다 (코드에서 알 수있는 한 적어도). 하나의 셀을 만들고 초기화됩니다. 요청 된 다른 데이터의 값 &으로 재설정되지 않습니다.

+0

그럴 필요가 없습니다. 이제 섹션의 0 행에있는 두 셀 모두 섹션 0의 셀 텍스트와 섹션 1 셀의 배경을 모두 얻습니다. 사고가 dequeueReusableCellWithIndentifier와 reuseIndentifier에 다른 식별자를 사용하고 있습니다. 이것은 나쁜 행동입니까? –

+0

원래 질문을 새 코드로 업데이트하십시오. 예, 식별자는 단일 테이블보기 내에서 동일해야합니다. –

+0

새 코드 –

0

코드가 잘못 구성되었습니다.

dqueueReuseableCellWithIdentifier는 이전에 만든 기존 셀을 더 이상 사용하지 않는 경우이를 반환합니다. If cell == nil 새 셀을 만들고 모든 셀에 기본값을 설정해야합니다. 그러나 해당 indexPath에 고유 한 데이터를 설정해야합니다.

if(cell==nil) block. 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1   reuseIdentifier:@"blahblahblah"] autorelease]; 
} 
[email protected]"unique text"; 
return cell; 
+0

Micheal Dautermann의 답변에 게시 한 의견을 참조하십시오. –