我试图根据我在TableView中选择的行将数据发送到DetailViewController。我的项目是用StoryBoard制作的。

我的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

    }

}

最佳答案

当您调用“performSegueWithIdentifier:sender:”而不是将自身传递给发件人时,请稍后传递所需的信息(即indexPath或由indexPath表示的模型对象)
稍后在prepareForSegue中(在if中),您可以将sender参数强制转换为先前传递的对象,并可以通过以下方式获取目标 View Controller :
[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”类应具有您需要的属性或ivars!
所以..如果您需要标签和图像,则您的类应如下所示:
@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

关于objective-c - didSelectedRowAtIndexPath-prepareForSegue- Storyboard,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9687741/

10-13 03:54