Tie*_*eme 2 json objective-c uitableview ios
因为我是Stackoverflow的新手,所以我无法评论某人.(我的声誉是16 ..).我有一个关于这个问题的问题:我如何将这个JSON数据放入我的表视图中?请帮助我,我生活在一场噩梦:)
Fulvio sais你必须使用 [eventNameList addObject:event];并[eventNameList objectAtIndex:indexPath.row];存储和获取事件数据,但是.addObject是一个NSMutableSet方法,而objectAtIndex:indexPath.row则不是.所以我无法使用此方法从NSMutableSet获取数据.
除此之外,我也不能使用计数方法.
有任何想法吗 ?
Wol*_*urs 15
假设您有一个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;
}
Run Code Online (Sandbox Code Playgroud)
@Tieme:显然您使用的URL已经返回一个数组,您不需要处理字典(您可以只使用该数组作为dataSource),请查看以下内容:
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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8642 次 |
| 最近记录: |