需要帮助将 Plist 加载到多个表中

发布于 2024-10-22 22:02:14 字数 4833 浏览 6 评论 0原文

我已成功将muscleName项目加载到表视图中,但没有运气将它们各自的exerciseName加载到第二个表视图中。

我使用的方法是:

    cell.textLabel.text = [[self.exerciseArray objectAtIndex:indexPath.row] objectForKey: @"exerciseName"];

也许我需要定义常量?

在此处输入图像描述

编辑:

这是 ViewDidLoad 中加载muscleName的第一个表的代码:

if (muscleArray == nil)
{
    //Read the plist file into an array (the root element is an array)
    NSString *path = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"plist"];
    NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
    self.muscleArray = rootLevel;
    [rootLevel release];

这是我当前的子表班级

 import "SpecificExerciseTableViewController.h"
    #import "MusclesTableViewController.h"
    #import "CurlAppDelegate.h"
    #import "DetailViewController.h"

    @implementation SpecificExerciseTableViewController
    @synthesize exerciseArray;

    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
        }
        return self;
    }

    - (void)dealloc
    {
        [exerciseArray release];
        [super dealloc];
    }

    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle
    - (void)viewDidLoad
    {
        self.navigationItem.title = @"Exercises";

        [super viewDidLoad];

        if (exerciseArray == nil)
        {
            //Read the plist file into an array (the root element is an array)
            NSString *path = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"plist"];
            NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
            self.exerciseArray = rootLevel;
            [rootLevel release];
        }

        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }

    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

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

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        NSUInteger row = [indexPath row];
        cell.textLabel.text = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exerciseName"];

        return cell;

    }

    - (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellAccessoryDisclosureIndicator;
    }

    #pragma mark - Table view delegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.

         DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
         [detailViewController release];

    }

    @end

I have successfully loaded the muscleName items into a table view but am not having any luck loading their respective exerciseName into the 2nd table view.

The method I am using is:

    cell.textLabel.text = [[self.exerciseArray objectAtIndex:indexPath.row] objectForKey: @"exerciseName"];

Maybe I need to define constants?

enter image description here

Edit:

Here is the code in ViewDidLoad for the first table that loads muscleName:

if (muscleArray == nil)
{
    //Read the plist file into an array (the root element is an array)
    NSString *path = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"plist"];
    NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
    self.muscleArray = rootLevel;
    [rootLevel release];

Here is my current sub table class

 import "SpecificExerciseTableViewController.h"
    #import "MusclesTableViewController.h"
    #import "CurlAppDelegate.h"
    #import "DetailViewController.h"

    @implementation SpecificExerciseTableViewController
    @synthesize exerciseArray;

    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
        }
        return self;
    }

    - (void)dealloc
    {
        [exerciseArray release];
        [super dealloc];
    }

    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle
    - (void)viewDidLoad
    {
        self.navigationItem.title = @"Exercises";

        [super viewDidLoad];

        if (exerciseArray == nil)
        {
            //Read the plist file into an array (the root element is an array)
            NSString *path = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"plist"];
            NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
            self.exerciseArray = rootLevel;
            [rootLevel release];
        }

        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }

    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

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

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        NSUInteger row = [indexPath row];
        cell.textLabel.text = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exerciseName"];

        return cell;

    }

    - (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellAccessoryDisclosureIndicator;
    }

    #pragma mark - Table view delegate

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.

         DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
         [detailViewController release];

    }

    @end

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

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

发布评论

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

评论(1

原野 2024-10-29 22:02:14

如果没有看到更多代码,如果您从 exercises 开始,那么您似乎可以正常访问 exerciseName 属性。
您确定 self.exerciseArray 指向您的 exercises 对象吗?最快的检查方法就是放入 NSLog() 语句,看看您是否拥有您认为拥有的内容:

NSLog(@"Exercises: %@", self.exerciseArray);

Without seeing more of your code, it looks like you're accessing the exerciseName property okay if you are starting at exercises.
Are you sure that self.exerciseArray is pointing to your exercises object? The quickest way to check is just drop in an NSLog() statement and see if you have what you think you have:

NSLog(@"Exercises: %@", self.exerciseArray);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文