gpt4 book ai didi

ios - plist 值未显示在 TableView 中

转载 作者:行者123 更新时间:2023-11-29 03:36:00 25 4
gpt4 key购买 nike

我有一个 plist,它是一个数组,其中包含字典。我试图在 TableView 中打印出名称,但名称没有显示。到目前为止我所拥有的是以下代码

    self = [super init];
if (self) {
// Fetch from the app bundle... load the NSDictionary
_qList = [NSArray arrayWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"plist"]];

_qNames=[_qList valueForKey:@"Name"];


}
return self;

对于我的表格 View ,我有

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


int index = [indexPath indexAtPosition:1];
NSString *key = [[self.test.qNames valueForKey:@"Name"] objectAtIndex:index];
NSString *value = [self.test.qNames objectForKey:key];
[[cell textLabel] setText:value];

return cell;
}

我的plist结构如下:

根(数组) -项目 0(字典) -姓名 -类(class) -年龄 -第 1 项(字典) -姓名 -类(class) -年龄

等等..

我只需要弄清楚如何放置

 NSDictionary *name=[self.model.qList valueForKey:@"Name"];

我返回 TableView 的名称?

最佳答案

我认为您正在尝试使用键值编码(KVC)从字典中获取名称(字符串)数组。

为此,您应该使用 valueForKeyPath ,而不是 valueForKey

在初始化中,

//Get the array of names
_qNames=[_qList valueForKeyPath:@"Name"];

在 cellForRowAtIndexPath 中,

NSString *name = [self.test.qNames objectAtIndex:indexPath.row];
[[cell textLabel] setText:name];

关于ios - plist 值未显示在 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19171228/

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