views:

61

answers:

3

Okay, so I have an app that I'm trying to get to populate a UITableView with the contents of a plist file. I haven't been able to find anyone else having problems doing this and how they fixed it so I'm probably just an idiot but I need to know how all the same!

I'm sure the error is somewhere in my viewDidLoad section or my tableview section...but maybe someone else can give me a better idea.

-(void)viewDidLoad {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"datam" ofType:@"plist"];
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
    self.listData = array;
    [array release];


    [super viewDidLoad];
}

#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *SimpleTableIdentifier = @"Identifier";

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

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    return cell;
}
A: 
self.listData = array; [array release];

Try this instead:

self.listData = [array copy]; [array release];

NSArray objects are pointers; when you set self.listData = array, you really tell anything looking for listData to look in the memory address originally occupied by array. When you release array, it vacates that memory address, thus making the pointer point to nothing.

Jason B
If the property is a retain though, as it probably should be, this isn't the issue.
David Kanarek
I think I agree with David K on this one, the property is set to retain so this SHOULDN'T be the issue...that being said I will probably run it with this change and see what happens anyways.Thanks for all the suggestions so far, I have class and will give this stuff a try later tonight.
Greenway
+1  A: 

I think [super viewDidLoad] should be foremost in the viewDidLoad method.

tomute
A: 
NSString *path = [[NSBundle mainBundle] pathForResource:@"datam" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

This seems a little odd; is your file named "datam.plist"?

Try using some NSLogs() throughout your code to see what the status of your array is at any given time:

NSLog(@"%@ has %i elements",ARRAY_OBJECT,[ARRAY_OBJECT count]);

Check the GDB output in XCode while the application runs to see if the results match what you expect.

Jason B
Okay, will try that.Yeah, my file is datam.plist
Greenway