准备ForSegue:发件人:获取错误的发件人



我有一个UITableView,它从表示标题,摘要和目标URL的字符串数组中获取数据。当用户选择单元格时,应用将转到包含 Web 视图的视图控制器。

问题是,无论选择哪个单元格,传递的数据始终是第一个单元格......

self.mockData是一个虚拟的硬连线数组,我一直在使用它,直到我开始处理模型)

下面的代码,非常感谢任何帮助。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"destinationWebView" sender:[self tableView:self.tableView cellForRowAtIndexPath:indexPath]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"destinationWebView"]) {
        MyCustomWebPageViewController *destinationViewController = [segue destinationViewController];
        if ([destinationViewController canPerformAction:@selector(setArticleUrl:) withSender:self]) {
            if (self.urlOut) {
                self.urlOut = nil;
            }
            NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
            NSString *urlString = [[self.mockData objectAtIndex:indexPath.row] objectAtIndex:2];
            self.urlOut = [NSURL URLWithString:urlString];
            [destinationViewController setArticleUrl:self.urlOut];
        }
    }
}

我认为用以下内容替换您的performSegueWithIdentifier行会起作用:

[self performSegueWithIdentifier:@"destinationWebView" sender:[self.tableView cellForRowAtIndexPath:indexPath]];

这在您现在向UITableView询问单元格时起作用,而不是UITableViewDataSource委托方法(如原始问题注释中也指出的那样)。

最新更新