0

我有一个通过情节提要segue推动UITableView一个视图,该视图显示一个UILabel我希望相indexPath.row对于.UITableView

我知道这可能大错特错,但这是我的尝试。我觉得我做错了:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"ArticlePreviewSegue" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = [sender indexPathForSelectedRow];
    ArticlePreviewViewController *apvc = [segue destinationViewController];
    NSDictionary *article = [_newsFetcher.articles objectAtIndex:indexPath.row];
    apvc.titleLabel.text = [article objectForKey:@"title"];
    apvc.bodyLabel.text = [article objectForKey:@"body"];
}

谢谢!

4

1 回答 1

2

一个问题可能是点击附件不会选择行。您可以通过将索引路径作为 segue 的发送者来处理:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:@"ArticlePreviewSegue" sender:indexPath];
}

现在您可以访问索引路径,prepareForSegue:sender:而无需依赖被选中的行。

另一个问题是 in prepareForSegue:sender:,apvc还没有加载它的视图。所以apvc.titleLabelapvc.bodyLabel都是零。

处理此问题的正确方法是提供ArticlePreviewViewController一个article属性并将该属性设置为prepareForSegue:sender:,如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = (NSIndexPath *)sender;
    ArticlePreviewViewController *apvc = [segue destinationViewController];
    apvc.article = [_newsFetcher.articles objectAtIndex:indexPath.row];
}

然后,在 中-[ArticlePreviewViewController viewDidLoad],您可以根据文章设置标签:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = self.article[@"title"];
    self.bodyLabel.text = self.article[@"body"];
}
于 2012-10-15T02:45:59.677 回答