从ios中的字典数组中的键获取内部数据
我有一本定义为从ios中的字典数组中的键获取内部数据,ios,objective-c,nspredicate,nsmutabledictionary,Ios,Objective C,Nspredicate,Nsmutabledictionary,我有一本定义为 @property (retain, nonatomic) NSMutableDictionary *MyDictionary; 我试图得到这样的输出 NSLog(@"%@",MyDictionary); 这是我得到的输出 { Category=( { code=12; Name="John Smith" }, { code=21; Name="Bobby Smith" },
@property (retain, nonatomic) NSMutableDictionary *MyDictionary;
我试图得到这样的输出
NSLog(@"%@",MyDictionary);
这是我得到的输出
{ Category=(
{
code=12; Name="John Smith"
},
{
code=21; Name="Bobby Smith"
},
{
code=31; Name="Smith Jones"
} );
Detail = {
code=1;
Text="Developer"
};
}
我的表格单元格中填入了名字,约翰·史密斯、鲍比·史密斯等等。我需要的是在单击特定单元格时获取代码。例如,如果我点击名字约翰·史密斯,我应该得到值12
这是到目前为止我的代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSArray *allNames = [MyDictionary allValues];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Name == %@", cell.textLabel.text];
NSArray *filteredNames = [allNames filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",filteredNames);
}
FilteredName不包含任何筛选对象。如何获取代码的值?已编辑:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *getCodeValue = [[[myDic objectForKey:@"Category"] objectAtIndex:indexPath.row] objectForKey:@"code"]
NSLog (@"%@", getCodeValue)
}
在编辑的didSelectRowAtIndexPath
方法中使用此代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *getCodeValue = [[[myDic objectForKey:@"Category"] objectAtIndex:indexPath.row] objectForKey:@"code"]
NSLog (@"%@", getCodeValue)
}
在
didSelectRowAtIndexPath
方法中使用此代码将代码分配给单元格的标记,并在单元格的didSelectRowAtIndexPath中使用它将代码分配给单元格的标记,并在单元格的didSelectRowAtIndexPath中使用它我建议创建一个UITableViewCell的子类,该子类只有一个标签和一个整数值作为属性。然后按如下方式初始化它:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellClassNameHere"];
cell.codeValue = [[myDic objectForKey:@"Category"] objectAtIndex:indexPath.row];
}
同时添加以下代码,以便在触摸单元格时检索代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *codeValue = cell.codeValue;
NSLog(@"%@", codeValue);
}
如上所述,请确保您的NSDictionary已正确初始化:
NSMutableDictionary myDic = [[NSMutableDictionary alloc] init]; //This can be replaced with however you're getting your code values.
那你该走了!应该注意的是,这个答案中没有测试任何代码,所以如果您有任何问题,请告诉我
Matt我建议创建一个UITableViewCell的子类,该子类只包含一个标签和一个整数值作为属性。然后按如下方式初始化它:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellClassNameHere"];
cell.codeValue = [[myDic objectForKey:@"Category"] objectAtIndex:indexPath.row];
}
同时添加以下代码,以便在触摸单元格时检索代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *codeValue = cell.codeValue;
NSLog(@"%@", codeValue);
}
如上所述,请确保您的NSDictionary已正确初始化:
NSMutableDictionary myDic = [[NSMutableDictionary alloc] init]; //This can be replaced with however you're getting your code values.
那你该走了!应该注意的是,这个答案中没有测试任何代码,所以如果您有任何问题,请告诉我
Matt正如您所提供的那样,这将基于什么获得codevalue,例如:如果我单击名为John Smith的单元格,它如何知道代码应该是12?@Gamerlegend-indexPath.row row return
{code=12;name=“John Smith”},
这是字典,有两个值1)12,其中键名为code,2)“John Smith”的键名是name,它非常简单:)NSLog(@“%@”,getCodeValue)仍然返回(null)NSLog(@“%@”,indexath.row)返回空值,我使用的是xcode 4.2,simulator 5.0。有什么建议吗?你确定要初始化你的dic吗?正如你所提供的那样,这将根据什么获得代码值,例如:如果我单击名为John Smith的单元格,它如何知道代码应该是12?@Gamerlegend-indexPath.row row return{code=12;name=“John Smith“},
这是字典,有两个值1)12,键名是code,2)“John Smith”,键名是name,它很简单:)NSLog(@“%@”,getCodeValue)仍然返回(null),NSLog(@“%@”,indexath.row)返回null值,我使用的是xcode 4.2,simulator 5.0有什么建议吗?您确定要初始化dic吗?您的表是否按MyDictionary输出中显示的“类别”、“详细信息”分组?或者您仅对表使用“类别”部分?仅对表使用类别部分,我想了解一下MyDictionary的外观。请显示设置cell tableView值的代码:cellForIndexPath:设置“name”的技术应该与获取“code”值的技术相同。您的表是否按照MyDictionary输出中显示的“Category”、“Detail”分组?或者您只是在表中使用“Category”部分?仅在表中使用Category部分,我想介绍一下MyDictionary的外观。请显示设置单元格表值的代码View:cellForIndexPath:设置“name”的技术与获取值“code”的技术应该是相同的。