2

TableViewで選択した行に従って、DetailViewControllerにデータを送信しようとしています。私のプロジェクトはストーリーボードで作られています。

TableViewCellはMainStoryBoardのUIViewに適切にリンクされています。選択した行の情報をDetailViewに送信する方法がわからない場合は、すべて問題ありません。

これが私が持っているものです:

- (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 

    }

}
4

2 に答える 2

9

自分自身を送信者に渡す代わりに「performSegueWithIdentifier:sender:」を呼び出すときは、後で必要な情報 (つまり、indexPath、または indexPath で表されるモデルのオブジェクト) を渡します) 後で prepareForSegue (if 内) でキャストできます先に渡したオブジェクトにsenderパラメーターを追加すると、[segue destinationViewController]; で送信先のView Controllerを取得できます。この時点で、目的のビュー コントローラーにいくつかのプロパティ (画像など) を設定できます。

そう...

- (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 = ....
}

編集:「DetailGrandComptes」クラスには、必要なプロパティまたは ivar が必要です! したがって、ラベルと画像が必要な場合、クラスは次のようになります。

@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
于 2012-03-13T16:45:32.153 に答える