0

我正在尝试在标签栏应用程序中开发一个带有情节提要的 RSS 阅读器钻取表。我已经设法用解析的 XML 填充我的 RootTableViewController。我现在在解决如何让我的 RootTableViewController 中的每一行指向并将数据从所选单元格传递到另一个 DetailTableViewController 时遇到问题。

这是我解析 XML 并填充 RootTableViewController 的代码的一部分:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [stories count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"AdvCurrentCelly";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    NSString *description =   [[stories objectAtIndex: storyIndex] objectForKey: @"description"];
    NSString *title = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];

    //This populates the prototype cell 'AdvCurrentCelly'
    cell.textLabel.text = title;
    //cell.textLabel.text = date;
    cell.detailTextLabel.text = description

    return cell;

}

在 Storyboard 中,从 RootTableViewContoller 单元格到 DetailTableViewController 的 segue 名称是ShowADVDetail

非常感谢帮助

4

1 回答 1

1

您可以传递任何类型的数据,但我将向您展示如何传递您的 Title 字符串。让我们称之为 myString。首先,您需要在 DetailTableViewController.h 中添加一个属性来存储您的字符串:

@property (strong, nonatomic) NSString *myString

在您的 RootTableViewController 中,您需要告诉 segue 要做什么。使用此代码作为示例:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Refer to the correct segue
    if ([[segue identifier] isEqualToString:@"ShowADVDetail"]) {

        // Reference to destination view controller
        DetailTableViewController *vc = [segue destinationViewController];

        // get the selected index
        NSInteger selectedIndex = [[self.teamTable indexPathForSelectedRow] row];

        // Pass the title (from your array) to myString in DetailTableViewController: 
        vc.myString = [NSString stringWithFormat:@"%@", [[stories objectAtIndex:selectedIndex] objectForKey: @"Title"]];
    }
}
于 2012-04-12T00:58:33.767 回答