iOS - How to display data from plist when there is more than one key?
I am able to get data from plist but it seems my cellForRowAtIndexPath is not getting called as none of NSLog printing anything from inside the method.
here is my code :-
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResou开发者_如何学Gorce:@"drinks" ofType:@"plist"];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
self.drinks = [tempDict objectForKey:@"Root"];
NSLog(@"%@", self.drinks);
NSLog(@"Number = %d", self.drinks.count);
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSLog(@"I am inside cellForRowAtIndexPath");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
NSArray *allValues = [self.drinks valueForKey:@"name"];
NSLog(@"%d", allValues.count);
cell.textLabel.text = [allValues objectAtIndex:indexPath.row];
return cell;
}
Here is my console output :-
2011-10-01 20:27:01.940 DrinkMixer[1832:b303] (
{
directions = "Shake adahsdk adlkasd";
ingredients = "kslkds lkjsjlkd kjsljlakj aajdlkj";
name = "Fire Cracker";
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = "Lemon Drop";
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = Mojito;
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = "After Drink Mint";
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = "Apple Martini";
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = "Shockwave";
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = "Beer";
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = "Last Desire";
},
{
directions = "abds fedfsdkkjkljlkj;j ok";
ingredients = "hasdhalsdlash asdhasldh kjdfkjshdj";
name = "Vodka";
}
)
2011-10-01 20:27:01.942 DrinkMixer[1832:b303] Number = 9
None of cellForRowAtIndexPath's NSLog get called.
Can anyone please tell what is wrong with this code.
Thanks.
Odds are that you either have not implemented UITableViewDelegate
in your header file, or your UITableView
's datasource
and delegate
are not wired up in your nib file.
I suggest checking out the datasource
.
UPDATE:
The fact that the tableView:cellForRowAtIndexPath:
isn't being called and the datasource
is set correctly then it could be that the plist is loaded into self.drinks
after the UITableView
is already loaded. Proof could be to scroll the empty UITableView
up and down and see if cells from off the screen start to appear.
I suggest two things:
call
[super viewDidLoad];
at the beginning of your-(void)viewDidLoad
call for a reload your UITableView after the plist is loaded.
Try:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
self.drinks = [tempDict objectForKey:@"Root"];
NSLog(@"%@", self.drinks);
NSLog(@"Number = %d", self.drinks.count);
[yourTableView reload];
}
Lastly, the reason I worry about your datasource
is because I think even an empty cell should call tableView:cellForRowAtIndexPath:
Hope this helps.
精彩评论