我有一个plist,它是一个数组,里面有字典。我试图打印出表视图中的名称,但是名称没有显示出来。到目前为止,我得到的是以下代码
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;对于我的表视图,我有
- (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结构如下:
根(数组) -Item 0(字典) -Name -Class -Age -Item 1(字典) -Name -Class -Age
以此类推..
我只需要弄清楚怎么把
NSDictionary *name=[self.model.qList valueForKey:@"Name"];我返回到表视图中的名称?
发布于 2013-10-04 13:22:38
我认为您正在尝试使用键值编码(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];https://stackoverflow.com/questions/19171228
复制相似问题