开发者

UITableView isn't loading the data from my plist file what am i doing wrong?

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 vie开发者_StackOverflowwDidLoad];
}

#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;
}


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜