UITableView仅在第二次尝试时将数据发送到Detail View Controller [英] UITableView only sending data to Detail View Controller on Second Try

查看:64
本文介绍了UITableView仅在第二次尝试时将数据发送到Detail View Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我一直在开发公交应用程序已有一段时间了,现在已经被这个问题困扰了一段时间了.

Hey there I have been working on a transit app for some time and have been stuck with this issue for a while now.

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

I am using iOS 5 and a storyboard. Basically I have a UITableView that displays favorite bus stop locations, when a user selects a row I use:

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

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

使用单元格的停靠点和路线信息,用户选择了我,然后准备与我的情节提要上的停靠点相对应的停靠点,并推动了一个使用停靠点名称和路线名称来显示来自plist的时间的详细视图控制器.

With the stop and route information of the cell the user chose I then prepare for a segue that corresponds to a segue on my storyboard, pushing a detail view controller that uses the stop name and route name to display times from a plist.

我对Objective C和iOS还是很陌生,所以我正在使用我的朋友告诉我可以使用的segue,但是,这可能是问题所在. segue看起来像这样:

I am fairly new to Objective C and iOS so I am using a segue that my friend told me would work, however, it might be the problem. The segue looks like this:

- (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中进行搜索后,我在视图DidLoad中抓取了停靠站并路线信息:

After the segue in my DetailViewController I grab the stop and route information in the 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);
}

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

Here is where my problems arise. When I run the simulator and click on an a cell in my table view, I am pushed to the DVC, however, the stopName and routeName are both null, so no information was sent or received. BUT, if I go back to the table and click another cell, the routeName and stopName are filled with the information that should have sent the first time I clicked a cell. If I continue this process it continues to send the information for the cell tapped previously, not currently.

因此,基本上是在发送信息,但只有在我两次进行了搜索之后才发送信息.显然,我希望它第一次发送和接收信息,但是它被延迟了,这让我很生气.非常感谢有人为我提供的任何帮助,因为我已经在互联网上搜索了几天来尝试解决此问题,在此先感谢您的帮助!

So basically information is sending but only after I go through the segue twice. Obviously I want it to send the information and receive it the first time, but it is delayed and driving me nuts. I appreciate any help someone can give me as I have been searching the internet for days now trying to fix this issue, thank you so much in advance for any assistance!

推荐答案

prepareForSegue:didSelectRowAtIndexPath:之前被调用.这就是为什么您看到的值始终滞后的原因.

prepareForSegue: is being called before didSelectRowAtIndexPath:. This is why the values you see always are lagging behind.

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

The better solution is to get the stopString and routeString values in your prepareForSegue: method (and not use didSelectRowForIndexPath: at all). The key to doing this is to realize that the sender parameter value being passed to prepareForSegue: is the UITableViewCell that was tapped. You can use the UITableView method indexPathForCell to get the cell's indexPath in your table, and then use that to look up the data in your favoriteItems array.

- (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"];
    }
}

这篇关于UITableView仅在第二次尝试时将数据发送到Detail View Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆