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 ...
}
! 정말 고마워! – TravelingAdMan
보기 컨트롤러간에 해당 데이터를 공유하는 가장 좋은 방법은 무엇입니까? – TravelingAdMan