将UITableViewCell中的plist推送到UIView

时间:2011-05-02 19:56:53

标签: iphone uitableview uiview plist pushviewcontroller

我有一个通过键“Title”编译UITableView的plist,plist在下面。表视图填充正确,我推动一个视图控制器:

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath;
{
    LocationsReviewViewController *nextViewController = [[LocationsReviewViewController alloc]init];
    NSDictionary *rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
    nextViewController.title = [rowData objectForKey: @"Title"];
    [self.navigationController pushViewController: nextViewController animated: YES];
}

如何获取上面的nextViewController以保留有关表格中哪个对象被选中的信息?此外,如何在新nextViewController视图中显示每个子树键中包含的字符串? ViewDidLoad应该是什么样的?

以下是ViewDidLoad目前用于nextViewController的内容(描述是UILabel)。当选择“LA Place 1”行时,为什么不显示@“3001 Sunset”:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"]];
    NSDictionary *subtree = [dictionary objectForKey: @"Subtree"];
    description.text = [NSString stringWithFormat:@"%@",[subtree objectForKey:@"Item 2"]];
}

<array>
    <dict>
        <key>Rows</key>
        <array>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Los Angeles, CA</string>
                    <string>Tuesday</string>
                    <string>3001 Sunset</string>
                </array>
                <key>Title</key>
                <string>LA Place 1</string>
            </dict>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>New York, NY</string>
                    <string>Sunday</string>
                    <string>1400 Broadway</string>
                </array>
                <key>Title</key>
                <string>NYC Place 1</string>
            </dict>
            <dict>
                <key>Subtree</key>
                <array>
                    <string>Austin, TX</string>
                    <string>Sunday</string>
                    <string>2400 Lamar Blvd</string>
                </array>
                <key>Title</key>
                <string>Austin Place 1</string>
            </dict>
        </array>
        <key>Title</key>
        <string>Section1</string>
    </dict>
</array>
</plist>

1 个答案:

答案 0 :(得分:0)

查看你的Data3.plist我看到它是一个包含2个项目的字典。

  • 第一项是带有密钥的NSArray:行
  • 第二项是带有键的NSString:Title

因此,在您的- (void)viewDidLoad中,您的代码不正确。这应该是正确的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data3" ofType:@"plist"]];
    // get the Rows object
    NSArray *rows = [dictionary objectForKey:@"Rows"];
    // The array object contains 3 NSDictionary
    NSDictionary *firstItem = [rows objectAtIndex:0]; // I am just getting the first item
    // once I have the dictionary, which have 2 items, Subtree & title 
    NSArray *subtree = [firstItem objectForKey: @"Subtree"];
    // subtree object is NSArray with 3 NSString object, therefore you have to use index
    description.text = [NSString stringWithFormat:@"%@",[subtree objectAtIndex:2]];
}

Data3.plist screenshot

现在,如果要将数据传递到下一个控制器,则应该向LocationsReviewViewController对象添加属性。

在LocationsReviewViewController.h中:

// declare an ivar
NSDictionary *data;

// declare property
@property (nonatomic, retain) NSDictionary *data;

在LocationsReviewViewController.m中:

@synthesize data;

分配LocationsReviewViewController对象时:

- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
{
    NSDictionary *rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
    LocationsReviewViewController *nextViewController = [[LocationsReviewViewController alloc]init];
    nextViewController.data = rowData;  // pass in your dictionary to your controller
    nextViewController.title = [rowData objectForKey: @"Title"];
    [self.navigationController pushViewController: nextViewController animated: YES];
}