UITableView仅在第二次尝试时将数据发送到Detail View Controller

Mas*_*gre 2 storyboard uitableview ios ios5 segue

嘿,我已经在一段时间内处理过境应用程序,并且已经暂时解决了这个问题.

我正在使用iOS 5和故事板.基本上我有一个UITableView,当用户选择我使用的行时,它显示最喜欢的公共汽车站位置:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row];

    stopString = favorite.title;
    routeString = favorite.subtitle;
}
Run Code Online (Sandbox Code Playgroud)

使用用户选择的单元格的停止和路径信息然后准备与我的故事板上的segue相对应的segue,推送使用停止名称和路径名称的详细视图控制器以显示来自plist的时间.

我对Objective C和iOS相当新,所以我使用的是我朋友告诉我的segue,但是,它可能是问题所在.segue看起来像这样:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destination = segue.destinationViewController;
    if ([destination respondsToSelector:@selector(setDelegate:)])
    {
        [destination setValue:self forKey:@"delegate"];
    }
    if ([destination respondsToSelector:@selector(setSelection:)])
    {
        NSString *route = routeString;
        NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, @"route", stopString, @"stop", nil];
        [destination setValue:selection1 forKey:@"selection"];
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的DetailViewController中的segue之后,我在视图DidLoad中获取停止和路由信息:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    route = [selection objectForKey:@"route"];
    stopName = [selection objectForKey:@"stop"];

    NSLog(@"stopName: %@", stopName);
    NSLog(@"routeName: %@", route);
}
Run Code Online (Sandbox Code Playgroud)

这是我的问题出现的地方.当我运行模拟器并单击表视图中的单元格时,我被推送到DVC,但是,stopName和routeName都是null,因此没有发送或接收信息.但是,如果我回到表并单击另一个单元格,则routeName和stopName将填充第一次单击单元格时应发送的信息.如果我继续此过程,它将继续发送之前未点击的单元格的信息,而不是当前.

所以基本上信息是发送,但只有在我经过两次segue之后.显然我希望它能够在第一时间发送信息并收到信息,但它会延迟并让我疯狂.我感谢有人可以给我的任何帮助,因为我已经在互联网上搜索了几天试图解决这个问题,非常感谢你提前帮助!

jon*_*oll 6

prepareForSegue:以前被称为didSelectRowAtIndexPath:.这就是为什么你看到的价值总是落后的原因.

更好的解决方案是在方法中获取stopString和routeString值prepareForSegue:(而不是根本使用didSelectRowForIndexPath:).这样做的关键是要意识到sender传递给的参数值是prepareForSegue:被轻击的UITableViewCell.您可以使用UITableView方法indexPathForCell在表中获取单元格的indexPath,然后使用它来查找favoriteItems数组中的数据.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{    
    UITableViewCell *cell = (UITableViewCell*)sender;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];   
    Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row];

    stopString = favorite.title;
    routeString = favorite.subtitle;

    UIViewController *destination = segue.destinationViewController;
    if ([destination respondsToSelector:@selector(setDelegate:)])
    {
        [destination setValue:self forKey:@"delegate"];
    }
    if ([destination respondsToSelector:@selector(setSelection:)])
    {
        NSString *route = routeString;
        NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, @"route", stopString, @"stop", nil];
        [destination setValue:selection1 forKey:@"selection"];
    }
}
Run Code Online (Sandbox Code Playgroud)