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

标签 ios uitableview ios5 storyboard segue

嘿,我已经在一个交通应用程序上工作了一段时间,现在已经被这个问题困扰了一段时间。

我正在使用 iOS 5 和 Storyboard。基本上我有一个 UITableView 显示最喜欢的公交车站位置,当用户选择我使用的行时:

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

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

有了用户选择的单元格的停靠站和路线信息,然后我准备了一个与我的 Storyboard上的一个 segue 相对应的 segue,推送一个使用停靠站名称和路线名称的详细 View Controller 来显示 plist 中的时间。

我对 Objective C 和 iOS 还很陌生,所以我正在使用我 friend 告诉我可以使用的 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"];
    }
}

在我的 DetailViewController 中进行 segue 之后,我在 View 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);
}

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

所以基本上信息正在发送,但只有在我经过两次 segue 之后才会发送。显然,我希望它在第一时间发送和接收信息,但它被延迟了,让我抓狂。我很感激有人能给我的任何帮助,因为我已经在互联网上搜索了好几天,试图解决这个问题,在此先感谢您的任何帮助!

最佳答案

prepareForSegue:didSelectRowAtIndexPath: 之前被调用。这就是为什么你看到的值(value)观总是落后的原因。

更好的解决方案是在 prepareForSegue: 方法中获取 stopString 和 routeString 值(根本不使用 didSelectRowForIndexPath:)。这样做的关键是要意识到传递给 prepareForSegue:sender 参数值是被点击的 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"];
    }
}

关于ios - UITableView 仅在第二次尝试时将数据发送到 Detail View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11980738/

相关文章:

ios - 将 cordova 项目从 3.3 更新到 4.x

ios - 将按钮 View 作为 subview 添加到标签 View 时,手势识别器不起作用

ios - 使用约束保持两个 TableView 具有相同的高度和相同的距离

ios - 如何子类化 UITableView?

objective-c - 就地编辑 UITableView 单元格

iOS 后台任务检测屏幕开启或关闭

ios - UISegmentedControl 的 3 段中的第一个占宽度的一半

iphone - 防止 UIButton 的 UIImage 大小调整

iphone - iOS:防止系统发送低内存警告时卸载后台定位服务

ios - 使用 UISplitViewController 而不是仅创建具有 2 个 subview 的单个 UIViewController 有什么好处