2012-08-26 6 views
2

[indexPath 행] 및 스크롤에 대해 다른 질문이 많이 있지만 이상한 숫자가 왜 반환되는지 이해할 수있는 사람이 누구인지 알고 있습니까? 로드 될 때cellForRowAtIndexPath - 스크롤 할 때 이상한 숫자를 반환하는 [indexPath 행]

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


    static NSString *CellIdentifier = @"ProfileIdentifier"; 

    NSUInteger row = [indexPath row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if(cell == nil){ 
     //My custom xib file is named "ProfileCell.xib" 
     [[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil]; 
       cell = profileCell; 
     self.profileCell = nil; 
    } 
//name and info are both of type NSArray 
    labelName.text = [name objectAtIndex:row]; 
    labelInfo.text = [info objectAtIndex:row]; 
    NSLog(@"Row: %d", row); 

    return cell; 
} 

콘솔이 반환됩니다 :

2012년 8월 26일 09 : 09 : 나는 내 세포를 구성하는 방식에 대한 해결 방법을 알아낼 수 없습니다 10.966 탐색 [ 8028 : F803] 행 0

2012년 8월 26일 09 : 09 : 10.983 탐색 [8028 : F803] 행 1

2012년 8월 26일 09 : 09 : 10.986 탐색 [8028 : F803] 행 : 2

2012-08-26 09 : 09 : 10.996 Nav [8028 : f803 ] 행 3

2012년 8월 26일 09 : 09 : 10.999 탐색 [8028 : F803] 행 4

2012년 8월 26일 09 : 09 : 11.002 탐색 [8028 : F803] 행 5 09 :

2012년 8월 26일 09

탐색 11.004 [8028 : F803] 행 6

2012년 8월 26일 09 : 09 : 11.007 탐색 [8028 : F803] 행 7

2012-08-26 09 : 09 : 11.010 탐색 [8028 : f803] 행 : 8

2012-08-26 09 : 09 : 11.012 탐색 [8

2012년 8월 26일 09 : 09 : 12.354 탐색 [8028 : F803] 행 3

028 : F803] 행 : I 셀 오프 스크린으로 스크롤 할 때 9

은 그러므로이 반환

2012년 8월 26일 09 : 09 : 12.404 탐색 [8028 : F803] 행 2

2012년 8월 26일 09 : 09 : 12.471 탐색 [8028 : F803] 행 1

2012-08 -26 09 : 09 : 12.622 Nav [8028 : f803] 행 : 0

내 실험실 elName.text 및 labelInfo.text는 사용자가 스크롤 할 때마다 변경됩니다.
그러나 영향을받는 셀은 맨 아래에만 있습니다. 9 번째 셀의 labelName을 [indexPath row]와 일관되게 유지하려면 어떻게해야합니까?

+1

labelName.text 대신 cell.labelName.text를 설정해야합니까? – jrturton

+0

U U tableview에 섹션이 있습니까? – tiguero

+0

@jrturton - 그렇게 생각하지 마십시오. 모든 세포가 의도 한대로 작동하고 있습니다. 마지막 셀만 변경되는 것이 문제입니다. – Magnum

답변

0

신경 쓰지 마, 주변에서 생각한 것 :

각 레이블에 태그를 설정했습니다. 설정 이유를 잘 모르겠어요 : X

편집 :이 결정하는 사람이 배열을 통해 라벨을 수정하는 데 도움이

UILabel *label; 
label = (UILabel *)[cell viewWithTag:1]; 
label.text = [name objectAtIndex:row]; 
label = (UILabel *)[cell viewWithTag:2]; 
label.text = [info objectAtIndex:row]; 

희망 : 다음과 같이 코드 편집 :

//labelName.text = [name objectAtIndex:row]; 
//labelInffo.text = [info objectAtIndex:row]; 

를 교체 태그는 작동하지만 내 원래 방법은 여전히 ​​라벨을 변경합니다. 누군가 설명 할 수 있습니까?

1

이 정의 UITableViewCell와 함께 작동하는 적절한 방법은 다음 .H 파일


:

: 당신의 하는 .m 파일

@interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    UINib *profileCellNib; 
} 

// ... 

@end 

- (void)viewDidLoad { 
    profileCellNib = [UINib nibWithNibName:@"ProfileCell" bundle:nil]; 
} 

// ... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ProfileIdentifier"; 

    NSUInteger row = [indexPath row]; 
    UIProfileCell *cell = (UIProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) cell = [[UINib instantiateWithOwner:self options:nil] objectAtIndex:0]; 

    cell.labelName.text = [name objectAtIndex:row]; 
    cell.labelInfo.text = [info objectAtIndex:row]; 

    NSLog(@"Row: %d", row); 

    return cell; 
} 

내가 대신 광산의 -tableView:cellForRowAtIndexPath: 방법에 올바른 이름을 설정해야 어쩌면 당신이 그 경우에 사용자 정의 셀을보기 위해 다른 이름을 사용하고는 UIProfileCellUITableViewCell에서 상속 된 클래스입니다 가정합니다.

1

전체 캐싱 메커니즘이 잘못 설정되었습니다. 여기

관련 코드 :

[[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil]; 
cell = profileCell; // profile cell is most likely nil here, you set cell to nil? 
self.profileCell = nil; // if profile cell wasn't nil, it will surely be nil next pass. 

난 당신이 -tableView:cellForRowAtIndexPath:에 초기화 코드를 기록 할 경우 모든 세포 대신 캐시에서 반환되는으로 다시됩니다 것을 볼 수 있습니다 내기. 당신 상황에서 초기화는 표시되는 첫 번째 셀에 대해 한 번만 수행되어야합니다. 다른 모든 셀에 대해 -dequeueReusableCellWithIdentifier: 메서드를 사용하여 캐시에서 디자인을 검색해야합니다.

+0

내 테이블 출력 셀에 대한 내 콘센트로 profile.cell을 내 .h 파일에 선언했습니다. @ 속성 (비 원자력, 강함) IBOutlet UITableViewController * profileCell profileCell은 nil이 아니며 올바르게 셀을 반환합니다. with loadNibNamed : @ "ProfileCell" – Magnum

관련 문제