将 plist 从 UITableViewCell 推送到 UIView

发布于 2024-11-04 19:28:40 字数 2788 浏览 0 评论 0原文

我有一个通过键“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];
}

How do I get nextViewController 上面保留一些有关选择表中哪个对象的信息?另外,如何在新的 nextViewController 视图上显示每个子树键中包含的字符串? ViewDidLoad 应该是什么样子?

这是 nextViewController 的 ViewDidLoad 当前的样子(描述是 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>

I have a plist that compiles a UITableView by the key "Title", the plist is below.The Table View populates correctly, I push a view controller with this:

- (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];
}

How do I get nextViewController above to retain some information about which object in the table was selected? In addition, how do I show the strings contained in each subtree key on the new nextViewController view? What should that ViewDidLoad look like?

Here's what it the ViewDidLoad currently looks like for the nextViewController (description is a UILabel). Why doesn't show @"3001 Sunset" when the "LA Place 1" row is selected:

- (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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2024-11-11 19:28:41

查看您的 Data3.plist,我发现它是一个包含 2 个项目的字典。

  • 第一项是带有键的 NSArray:Rows
  • 第二项是带有键的 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 snapshot

现在,如果您想将数据传递到下一个控制器,您应该添加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];
}

Looking at your Data3.plist I see it's a dictionary of 2 items.

  • First item is NSArray with a key: Rows
  • Second item is a NSString with a key: Title

So in your - (void)viewDidLoad your code is incorrect. This should be the correct code:

- (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

Now, if you want to pass data to the next controller you should add a property to your LocationsReviewViewController object.

In your LocationsReviewViewController.h:

// declare an ivar
NSDictionary *data;

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

In your LocationsReviewViewController.m:

@synthesize data;

When you alloc the LocationsReviewViewController object:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文