Search code examples
iosobjective-cuitableviewplist

Populating TableView with Plist Documents Directory in iOS


  • I'm a newbie to Objective-C so any help is greatly appreciated.
  • I have a TableView that is successfully pulling back the list from my Data.plist file within my Xcode project but I need it to pull from the Documents Directory.
  • I have seen many posts about this but can't seem to get it to work for me.
  • Here is my .m file below. Like I said, it pulls back the data but I need it to be dynamically changed based on my Documents plist copy.

Thanks in advance!

 @synthesize content = _content;

 - (NSArray *)content
{
    if (!_content) {
        _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
    }
    return _content;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.content count];
}

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

    // Configure the cell...
    cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"Name"];
    cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"Score"];

    return cell;
}

Solution

  • Try this:

    Step 1: In .h File declare a global variable

    @property (nonatomic,retain) NSMutableDictionary *contents;
    

    Step 2: In .m file set synthesis.

    @synthesize contents;
    

    Step 3: Move plist file from mainbudle into documents directory

    -(void) createPlistDocuments
    {
        // Get path to documents directory
        NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        // Finds the contained Documents directory
        NSString *documentsDirectory = [arrayPaths objectAtIndex:0];
        NSError *error;
        // Create an object that we will later use to look for a file and return a boolean value on whether or not it exists
        NSFileManager *manager = [NSFileManager defaultManager];
        // File we want to move, stored in original top level directory
        NSString *demoFile = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
        // Define where we want it moved to and name it
        NSString *demoFileMoved = [NSString stringWithFormat:@"%@/data.plist", documentsDirectory];
        // Attempt the copy
        if ([manager copyItemAtPath:demoFile toPath:demoFileMoved error:&error] != YES)
        NSLog(@"Unable to move file: %@", [error localizedDescription]);
    }
    

    Step 4: Read plist data from document directory

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self createPlistDocuments];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(
        NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];
        NSString *path = [NSString stringWithFormat:@"%@/data.plist",documentsDirectoryPath];
        contents = [NSArray arrayWithContentsOfFile:path];
        NSLog(@"%d", contents.count);
    
    }
    

    For brief explanation follow this sample... TechDevMobile(IOS-Message-Chat)