Objective c NSDictionary到TableView
因为我是Stackoverflow的新手,所以我还不能评论别人。(我的名声是16…)。我有一个关于这个anwser的问题: sai您必须使用Objective c NSDictionary到TableView,objective-c,json,ios,uitableview,Objective C,Json,Ios,Uitableview,因为我是Stackoverflow的新手,所以我还不能评论别人。(我的名声是16…)。我有一个关于这个anwser的问题: sai您必须使用[eventnamelistaddobject:event]和[eventnamelistobjectatindex:indexPath.row]以存储和获取事件数据,但是。addObject是NSMutableSet方法,objectAtIndex:indexPath.row不是。因此,我无法使用此方法从NSMutableSet获取数据 除此之外,我也不能
[eventnamelistaddobject:event]
和[eventnamelistobjectatindex:indexPath.row]
以存储和获取事件数据,但是。addObject是NSMutableSet方法,objectAtIndex:indexPath.row不是。因此,我无法使用此方法从NSMutableSet获取数据
除此之外,我也不能使用计数方法
有什么想法吗?幸运的:)
假设您可能拥有nsdictionary的nsmutablearray
在这种情况下,您可以使用以下方法获取数据:
[dictionary objectforkey:@"key"] objectAtIndex:indexpath.row]
假设您有一个NSDictionary,您可以使用[dictionary allKeys]方法检索一个包含所有键的数组(现在称之为keyArray)。对于rowCount,您可以返回此keyArray中对象的计数。要获取需要在单元格中显示的项,可以使用[dictionary objectForKey:[keyArray objectAtIndex:indexPath.row]]为显示的单元格获取适当的字典 代码:
// use the keyArray as a datasource ...
NSArray *keyArray = [jsonDictionary allKeys];
// ------------------------- //
// somewhere else in your code ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [keyArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// set some cell defaults here (mainly design) ...
}
NSString *key = [keyArray objectAtIndex:indexPath.row];
NSDictionary *dictionary = [jsonDictionary objectForKey:key];
// get values from the dictionary and set the values for the displayed cell ...
return cell;
}
@Tieme:显然,您使用的URL已经返回了一个数组,您实际上不需要处理字典(您可以将该数组用作数据源),请查看以下内容:
SBJSON *json = [[[SBJSON alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:@"http://www.my-bjoeks.nl/competitions/fetchRoutes/25.json"];
NSString *string = [[[NSString alloc] initWithContentsOfURL:url] autorelease];
NSError *jsonError = nil;
id object = [json objectWithString:string error:&jsonError];
if (!jsonError) {
NSLog(@"%@", object);
NSLog(@"%@", [object class]); // seems an array is returned, NOT a dictionary ...
}
// if you need a mutableArray for the tableView, you can convert it.
NSMutableArray *dataArray = [NSMutableArray arrayWithArray:object]
eventNameList
应定义为NSMutableArray
,而不是NSMutableSet
NSMutableArray
同时响应-addObject
(它将新对象放在数组的末尾)和-objectAtIndex:
,仔细想想,表视图本质上是一个有序列表,数组也是一个有序列表,而集合则不是。这看起来像easies解决方案,但对我来说不起作用,tableview仍然为空,当我使用count方法对numberOfRowsInSection方法的数据进行计数时,我返回0@Tieme:在count方法中放置一个断点,并检查数组是否为零。@Tieme:在要停止的行上放置一个断点(单击编辑器窗口的空白处),然后在调试器中运行程序。谢谢,刚刚找到它。数组确实为零,我将要找出..不起作用的原因:**-[[uuu nsarray-valueForKey:]:发送到已解除分配实例0x4b72c80cell的消息。text=[NSString-stringWithFormat:@“%@,[[arrayList-objectAtIndex:indexath.row]objectForKey:@“keyname”];(arrayList是我的nsmutablearray,keyname是key..您需要将arrayList设置为类变量..这对我来说很好…NSArray*keyArray=[jsonDictionary allKeys];给出以下错误:-[\u NSArrayM allKeysForObject:]:发送到实例0x4E4E660的无法识别的选择器是否确实在NSDictionary对象上调用-allKeys方法?因为此方法本身可用于任何NSDictionary,但收到的错误表明您在另一种类型的对象上调用它,请参阅:此操作无效:-(void)viewDidLoad{[super viewDidLoad];//从json对象NSString*jsonObject=[[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:@]加载;//为了解析它,我制作了一个sbjsonparser对象NSError*jsonError;NSDictionary*parsedJSON=[[sbjsonparser new]init]对象WITHSTRING:jsonObject错误:&jsonError];//使用keyArray作为数据源…NSArray*keyArray=[parsedJSON allKeys];}
@wolfgang schreurs谢谢,这就实现了!