UITableView详细说明使用故事板向下钻取UITableView

时间:2012-04-05 11:56:35

标签: objective-c ios storyboard segue

我正在尝试使用Tab栏应用程序开发一个带有故事板的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

非常感谢

1 个答案:

答案 0 :(得分:1)

您可以传递任何类型的数据,但我会告诉您如何传递您的标题字符串。让我们称之为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"]];
    }
}