gpt4 book ai didi

iphone - iOS - 当有多个键时如何显示 plist 中的数据?

转载 作者:行者123 更新时间:2023-11-29 04:57:42 24 4
gpt4 key购买 nike

我能够从 plist 获取数据,但似乎我的 cellForRowAtIndexPath 没有被调用,因为 NSLog 没有从方法内部打印任何内容。

这是我的代码:-

- (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 ,或者您的 UITableViewdatasourcedelegate 未连接到您的 nib 文件中。

我建议检查数据源

更新:

事实上,tableView:cellForRowAtIndexPath: 没有被调用,并且 datasource 设置正确,那么 plist 可能已加载到 self 中加载 UITableView 后的 .drinks 。证据可能是上下滚动空的 UITableView 并查看屏幕外的单元格是否开始出现。

我建议两件事:

  1. -(void)viewDidLoad 开头调用 [super viewDidLoad];

  2. 在加载 plist 后调用重新加载 UITableView。

尝试:

- (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];

}

最后,我担心您的数据源的原因是因为我认为即使是空单元格也应该调用tableView:cellForRowAtIndexPath:

希望这有帮助。

关于iphone - iOS - 当有多个键时如何显示 plist 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7621731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com