Search code examples
iosuitableviewplist

iOS Populating UITableView with Data from Plist


I am trying to populate a UTTableView with the contents of my Data.plist file. I need to load this into an NSMutableArray. This is how far I came:

(View Did Load):

    NSString *PlistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

    Array = [[NSMutableArray alloc] initWithContentsOfFile:PlistPath];

    [Array addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Testing 1", @"name", nil]];

And of course at CellForRowAtIndexPath:

cell.textLabel.text = [Array objectAtIndex:indexPath.row];

I wrote this in my Data.plist file (picture):

https://i.sstatic.net/Cs1xd.png

Now when I run the App. My TableView remains empty.

Thanks for your time and help!


Solution

  • Currently your plist's root is set to Dictionary. Change that to array first.

    Then use this code to show the data:

    cell.textLabel.text = [[Array objectAtIndex:indexPath.row] objectForKey:@"name"];