Table View Won't Load Data

Discussion in 'iOS Programming' started by nashyo, Mar 4, 2012.

  1. macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    I have a project that loads an array into a table view, no problem at all. I have created a new project, that has the exact same code, but a different array, and the table view loads blank. It's driving me nuts.

    The array loads a plist as follows

    where 'currentContent is a strong pointer to an NSArray private property of the parent view.
    Code:
    - (void) viewDidLoad {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CustomerContent" ofType:@"plist"];
        NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:path];
        currentContent = myArray;
    }
    the plist looks like this
    [​IMG]

    table view data source
    Code:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    
    {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return [currentContent 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];
        }
       
        cell.textLabel.text = [[currentContent objectAtIndex:indexPath.row] objectForKey:@"customerFriendlyFileName"];
        cell.detailTextLabel.text = [[currentContent objectAtIndex:indexPath.row] objectForKey:@"publicationDate"];
        return cell;
    }
    the data source is connected in storyboard. the cell reuse identified in the storyboard is named as it is in the code. the above code has been entered in a subclass of uitableviewcontroller.

    the storyboard is simply a tableview embedded in a navigation controller. the navigation controller is the initial view controller.

    I have NO IDEA why this isn't working. Please help.
     
  2. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #2
    It is defiantly something to do with my plist. The table view will populate with an NSArray object contaning three strings @"One", @"Two", @"Three", but not the plist.

    Why isn't the plist being recognised?
     
  3. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    The table view is now loading from my plist, but I had to create the plist in code, by writing it into the doc directory of the application sandbox THEN copy and paste it from the doc directory in iPhone simulator to application bundle.

    By going to Xcode ->new file -> plist and manually entering the data there, the plist wasn't loading. What's that about?
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Probably the difference is whether the root of the plist is an array or a dictionary.
     
  5. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    Basic question...how do I change that?
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    If you create the plist in code then it's entirely up to your code. If you create the plist in Xcode it does it in a standard way. Once the plist is created and you're editing it in the plist editor or in Xcode you can change it to whatever you like. The main thing of course is to be able to tell what the root of the plist is in the editor and then make sure it's what you want it to be.

    In the screenshot you show it looks like Item 0 is a dictionary. I assume that Item 0 is in an array.

    This might be more clear to you if you open the plist file in a text editor where you can see the xml.
     
  7. macrumors 6502

    Joined:
    Jun 19, 2009
    #7
    Look in the plist editor in Xcode again. Look under Type on the line for Item 0. If you click where it says Dictionary, you will get a drop down list that lets you change it to an Array.

    But, I'm not sure you want to do that because you already have keys and values that are related; and in your cellForRowAtIndexPath method your are retrieving values based on those keys.

    If I were you, I would change my viewDidLoad method so that you load the contents of your plist into a NSDictionary instead of a NSArray.

    Hope it works out.:cool:
     
  8. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #8
    Thanks for the reply. However, I need an array to run the table.

    Anyway, I generated a plist list that looks identical and now it works (check my response above).
     

Share This Page