我能夠從plist獲取數據,但似乎我的cellForRowAtIndexPath沒有被調用,因爲沒有NSLog打印方法內的任何內容。iOS - 當有多個鍵時,如何顯示來自plist的數據?
這裏是我的代碼: -
- (void)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);
[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;
}
這裏是我的控制檯輸出: - 的cellForRowAtIndexPath的的NSLog的
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
沒有被調用。
任何人都可以請告訴這段代碼有什麼問題。
謝謝。
這段代碼沒有錯,所以你的錯誤一定在別的地方。你的tableView方法是否被調用?例如,有多少行被返回? – Jamie
我已經解決了這個問題。我犯了一個非常愚蠢的錯誤。我從方法numberOfRowsInSection返回錯誤的值。感謝您試圖幫助我。對此,我真的非常感激。 – Varundroid