【发布时间】:2013-10-19 12:02:04
【问题描述】:
我有一个来自 json 的 nsdictionary,它的响应是:
({
email = "something@gmail.com";
name = "User1";
},
{
email = "something2@gmail.com";
name = "user2";
})
这是我在 de .m 文件中的代码:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
int errorCode = httpResponse.statusCode;
NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString];
NSLog(@"response is %d, %@", errorCode, fileMIMEType);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//NSLog(@"data is %@", data);
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(@"string is %@", myString);
NSError *e = nil;
usersDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
NSLog(@"dictionary is %@", usersDictionary);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NombreUsuario = [[NSMutableArray alloc] initWithCapacity:[usersDictionary count]];
}
我用它来返回section中的行数:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [usersDictionary count];
}
但我不知道如何将这对数据 email,name 放入 tableview 中,其中每个单元格都必须显示名称的主标签和电子邮件的副标题。
提前致谢。
【问题讨论】:
-
您显示的 JSON 是一个字典数组...您的具体问题是什么?
-
这确实是一个基本问题。鉴于您意识到您有一个
NSArray的NSDictionary对象(正如 Wain 已经指出的那样),它非常适合表格视图数据源,您将在这里找到答案:Table View Programming Guide for iOS,尤其是章节“仔细查看表格视图单元格”。 -
好吧,很抱歉问了这样一个基本问题,但我找不到如何从 de 字典中获取 nsarray,我总是定位一个“[__NSArrayM objectForKey:]: unrecognized selector sent to instance”我不知道我真的不知道如何处理这个问题,我已经阅读了两天的所有网络。
标签: ios json uitableview nsdictionary