Iphone iOS-当有多个键时,如何显示plist中的数据?
我可以从plist获取数据,但似乎我的cellforrowatinedexpath没有被调用,因为NSLog没有从方法内部打印任何内容 这是我的密码:-Iphone iOS-当有多个键时,如何显示plist中的数据?,iphone,ios,plist,Iphone,Ios,Plist,我可以从plist获取数据,但似乎我的cellforrowatinedexpath没有被调用,因为NSLog没有从方法内部打印任何内容 这是我的密码:- - (void)viewDidLoad { NSString *path = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"]; NSMutableDictionary *tempDict = [[NSMutableDictionary allo
- (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;
}
以下是我的控制台输出:-
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
未调用cellForRowAtIndexPath的NSLog
谁能告诉我这个代码有什么问题吗
谢谢。可能是您没有在头文件中实现
UITableViewDelegate
,或者您的UITableView
的datasource
和delegate
没有连接到nib文件中
我建议查看数据源
更新:
如果没有调用tableView:cellforrowatinexpath:
,并且datasource
设置正确,那么可能是在加载UITableView
之后,plist被加载到了self.drinks
。证据可能是向上和向下滚动空的UITableView
,查看屏幕外的单元格是否开始出现
我建议两件事:
[super viewDidLoad]代码>在-(void)viewDidLoad的开头
数据源的原因是因为我认为即使是空单元格也应该调用tableView:cellforrowatinexpath:
希望这有帮助。一切都在正确的位置。这段代码以前可以工作,但我对plist做了一些更改,之后它就停止工作了,尽管我仍然能够从plist获取数据,正如您可以看到的控制台跟踪。我不知道我做错了什么。我终于解决了这个问题。多么愚蠢的错误啊。该死的!!我从numberOfRowsInSection返回了一个错误的值。谢谢你试着帮助我。我真的很感激,酷。很高兴你找到了答案。这段代码没有错,所以你的错误一定在别的地方。是否调用了任何tableView方法?例如,返回了多少行?我已经解决了这个问题。我犯了一个愚蠢的错误。我从方法numberOfRowsInSection返回了错误的值。谢谢你试着帮助我。我真的很感激。
- (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];
}