2011-04-12 4 views
0

셀 셀을 채우는 배열에서 데이터를 전달하는 방법을 보여줄 수있는 튜토리얼을 찾았습니다. .textlabel.text 필드에도 해당 노드에 추가 정보가 있습니다.아이폰 SDK - UITableViewCell이 선택되었을 때 UITableViewCell에 표시되지 않는 배열에 데이터 전달하기

XML 파일을 생성하는 Webservice에서 구문 분석을하고 있습니다. "ID", "이름", "기타 ID"필드를 반환합니다. 그런 다음 구문 분석 된 XML로 UITableViewController를 채우는 것은 본질적으로 cell.textLabel.text의 "Name"필드입니다. 나는이 모든 일을해야한다.

그러나, 지금 일이 필요하다 :

사용자 변수로 설정하는 나는 "ID"와 (셀에 표시되지 않습니다) "OtherID"필드가 필요 행을 선택 않는 경우 뷰를 전환하고 이전 뷰의 변수를 사용하여 새 뷰의 UILabel을 설정합니다.

그리고 난 손실에있어 곳이다 ...

사람이 코드가 수행 할 수 방법에 대한 튜토리얼 또는 코드 예제의 방향으로 날 지점 수 있을까요? 아니면 저와 비슷한 비슷한 것을 가지고 계신가요?

감사합니다.

답변

1

tableView:cellForRowAtIndexPath:에 UITableViewCell을 설정하면 셀의 색인 경로를 기반으로 결과 배열의 특정 색인에서 데이터를 사용하게됩니다.

가장 쉬운 방법은 선택한 셀의 인덱스 경로를 사용하고 위와 동일한 계산을 수행하여 특정 인덱스를 찾는 것입니다. 그런 다음 원래 배열에서 필요한 모든 것을 가져올 수 있습니다. 즉,이 같은 보일 것이다 : 방법은 당신이 필요합니다 데이터를 보유하는 속성 또는 인스턴스 변수를 추가 할 수있는 UITableViewCell 하위 클래스하는 것

- (NSUInteger)arrayIndexFromIndexPath:(NSIndexPath *)path { 
    // You could inline this, if you like. 
    return path.row; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path { 
    NSUInteger index = [self arrayIndexFromIndexPath:path]; 
    NSDictionary *data = [self.dataArray objectAtIndex:index]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...]; 
    if (!cell) { 
     cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:...] autorelease]; 
    } 

    // ... Set up cell ... 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path { 
    NSUInteger index = [self arrayIndexFromIndexPath:path]; 
    NSDictionary *data = [self.dataArray objectAtIndex:index]; 

    // ... Do whatever you need with the data ... 
} 

약간 더 복잡한 (하지만 아마도 더 강력한을); 이는 원래 배열에서 전체 값을 유지하는 단일 속성만큼 간단 할 수 있습니다. 그런 다음 선택 메소드에서 셀 자체에서 필요한 모든 것을 가져올 수 있습니다. 즉,이 같은 것을 보일 것이다

MyTableViewCell.h :

@interface MyTableViewCell : UITableViewCell { 
    NSDictionary *data_; 
} 

@property (nonatomic, retain) NSDictionary *data; 

@end 

MyTableViewCell.m : 다음

@implementation MyTableViewCell 

@synthesize data=data_; 

- (void)dealloc { 
    [data_ release]; data_ = nil; 
    [super dealloc]; 
} 

@end 

를 실행 한 다음, 그것을 완벽하게 수행되었다

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path { 
    NSUInteger index = [self arrayIndexFromIndexPath:path]; 
    NSDictionary *data = [self.dataArray objectAtIndex:index]; 

    // Remember, of course, to use a different reuseIdentifier for each different kind of cell. 
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...]; 
    if (!cell) { 
     cell = [[[MyTableViewCell alloc] initWithStyle:... reuseIdentifier:...] autorelease]; 
    } 

    cell.data = data; 

    // ... Set up cell ... 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path { 
    MyTableViewCell *cell = [tableView cellForRowAtIndexPath:path]; 
    NSDictionary *data = cell.data; 

    // ... Do whatever you need with the data ... 
} 
+0

! 정말 고마워! – TravelingAdMan

+0

보기 컨트롤러간에 해당 데이터를 공유하는 가장 좋은 방법은 무엇입니까? – TravelingAdMan