2012-03-13 6 views
2

TableView에서 선택한 행에 따라 DetailViewController에 데이터를 보내려고합니다. 내 프로젝트는 StoryBoard로 만들어졌습니다.didSelectedRowAtIndexPath - prepareForSegue - Storyboard

내 TableViewCell 잘 내 MainStoryBoard 내 UIView의 연결되어, 모든 것이 내가 내 DetailView 여기

선택한 행의 정보를 전송하는 방법을 모르는 것으로 기대 좋은 것은 내가 무엇을 가지고 :

"보낸 사람 : performSegueWithIdentifier"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 
    NSUInteger oldRow = [lastIndexPath row]; 
    NSString *key = [keys objectAtIndex:section]; 
    detailRow = [names objectForKey:key]; 
    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 
    if (cell == nil) 
    { 
     CGRect cellFrame = C 

GRectMake(0, 0, 300, 65); 
     cell = [[UITableViewCell alloc] initWithFrame:cellFrame] ; 
    } 
    CGRect nameLabelRect = CGRectMake(200, 5, 200, 15); 
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect]; 
    nameLabel.tag = kNameValueTag; 
    [cell.contentView addSubview: nameLabel]; 

    UILabel *name = (UILabel *)[cell.contentView viewWithTag:kNameValueTag]; 
    name.text = [[detailRow objectAtIndex:row] valueForKey:@"name"]; 

    cell.imageView.image = [UIImage imageNamed:[[detailRow objectAtIndex:row] valueForKey:@"image"]]; 

    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? 
     UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

    return cell; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    [self performSegueWithIdentifier:@"DetailGrandComptes" sender:self]; 
} 



    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    { 

    if ([[segue identifier] isEqualToString:@"DetailGrandComptes"]) 
    { 

     // HERE IS WHERE THE CODE SHOULD BE I GUESS.... !! I HAVE TO DISPLAY AN UIIMAGEVIEW + UILABEL + UIIMAGEVIEW 

    } 

} 

답변

9

당신이 전화 대신 보낸 사람에게 자기를 전달 나중에 필요한 정보를 전달 (즉, 하나 indexPath, 또는 indexPath으로 표시 모델의 객체) 나중에 prepareForSegue에서 (내부 사용자 if) 이전에 전달한 객체에 보낸 사람 매개 변수를 캐스팅 할 수 있습니다. [segue destinationViewController]로 대상보기 컨트롤러를 얻을 수 있습니다. 당신이 당신의 목적지 뷰 컨트롤러에 대한 몇 가지 속성을 설정할 수 있습니다이 시점에서 ..

그래서 ...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"DetailGrandComptes" sender:indexPath]; 
} 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 

if ([[segue identifier] isEqualToString:@"DetailGrandComptes"]) 
{ 
    MyViewController *destination = [segue destinationViewController]; 
    NSIndexPath * indexPath = (NSIndexPath*)sender; 
    destination.imageName = [[detailRow objectAtIndex:indexPath.row] valueForKey:@"image"] 
    destination.nameString = .... 
} 

EDIT (이미지, 등 등) : 귀하의 "DetailGrandComptes"클래스의 속성 또는 인스턴스 변수가 있어야합니다 너 필요해! 그래서 .. 당신은 라벨과 이미지를 필요로하는 경우, 클래스는 다음과 같이해야한다 :

@interface DetailGrandComptes : UIViewController 

@property (strong, nonatomic) NSString *imageName; 
@property (strong, nonatomic) NSString *nameString; 

@end 

//in you .m 
@interface DetailGrandComptes() 
@property (weak, nonatomic) IBOutlet UIImageView *image; 
@property (weak, nonatomic) IBOutlet UILabel *name; 
@end 

@implementation DetailGrandComptes 

- (void)viewWillAppear 
{ 
    [super viewWillAppear]; 
    name.text = nameString; 
    image.image = [UIImage imageNamed:imageName]; 
} 

@end 
+0

난 그냥, 내 대답을 게시,하지만 모든 : P – user1256827

+0

@ user1256827 내 대답의 편집을 볼 ... – Francesco

+1

이 솔루션은 제대로 prepareForSegue를 사용하지 않습니다. 보낸 사람은 다른 정보가 아닌 Segue를 시작하는보기 컨트롤러 여야합니다. prepareForSegue에 도달하기 전에 deselectRowAtIndexPath가 호출되지 않고 tableView에서 indexPathForSelectedRow를 사용할 수 있는지 확인하십시오. – Johannes

0

게다가, 대상 뷰 컨트롤러는 네비게이션 뷰 컨트롤러 인 경우. 는해야한다 : 그것의 절반이 작동하기 때문에

MyViewController *destination = [[segue destinationViewController] topViewController];