如何将 json 文件数据显示到 UICollectionView
How to display json file data to UICollectionView
在这里,我尝试从我的 URL
中获取数据,使用 base64,shaa256 进行授权 Header,我只是试图在我的控制台中显示。它运作良好。但我需要转换为 NSDictionary
并显示到我的 UICollectionView
。我的数据有 3 sections
个不同的数据。每个部分有 12 - 20 个文件。
所以我的问题是:
- 如何将我的 url 转换为 json 到 NSDictionary?
- 要显示 3 个部分并且每个部分应该有 12 到 20 个数据文件吗?
- 我在 viewdidload 中只使用了这段代码来获取 data.does 我需要将
1.Connection did receive response 2.Connection did receive data 3.connection didFailWithError 4.connectionDidFinishLoading
添加到我的代码中,或者不希望将这 4 种方法添加到我的代码中
这是我的代码:
我曾经在
之后将下面的代码插入到上面的代码中
NSString *str = [[NSString alloc] initWithData:returnData
encoding:NSUTF8StringEncoding];
NSError * error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:str
options: NSJSONReadingMutableContainers
error: &error];
NSLog(@"%@",json);
但它 work.if 对我的第 3 个问题没有任何有用的答案对 me.i 我 ios.help 我 out.Thanks 的新手非常有用 out.Thanks !
数据很可能是 JSON 而不是纯文本?所以将响应直接转换为 NSDictionary
:
// show loading indicators when you are fetching data
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// hide loading indicators when you received a response
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (connectionError) {
// Handle error
} else {
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"jsonResults: %@", jsonResults);
// reload your views
// [self.myCollectionView reloadData];
}
}];
要制作包含 3 个部分的 collectionView,您需要一个包含 3 个部分的 NSArray,这 3 个数组中的每一个都将包含 12-20 个字典,其中的键和值将填充每个单元格。
NSArray *sectionOne = @[@{@"Section":@"1", @"Object":@"1"}, @{@"Section":@"1", @"Object":@"2"}];
NSArray *sectionTwo = @[@{@"Section":@"2", @"Object":@"1"}, @{@"Section":@"2", @"Object":@"2"}];
NSArray *sectionThree = @[@{@"Section":@"3", @"Object":@"1"}, @{@"Section":@"3", @"Object":@"2"}];
NSArray *collectionData = @[sectionOne, sectionTwo, sectionThree];
NSLog(@"collectionData: %@", collectionData);
在这里,我尝试从我的 URL
中获取数据,使用 base64,shaa256 进行授权 Header,我只是试图在我的控制台中显示。它运作良好。但我需要转换为 NSDictionary
并显示到我的 UICollectionView
。我的数据有 3 sections
个不同的数据。每个部分有 12 - 20 个文件。
所以我的问题是:
- 如何将我的 url 转换为 json 到 NSDictionary?
- 要显示 3 个部分并且每个部分应该有 12 到 20 个数据文件吗?
- 我在 viewdidload 中只使用了这段代码来获取 data.does 我需要将
1.Connection did receive response 2.Connection did receive data 3.connection didFailWithError 4.connectionDidFinishLoading
添加到我的代码中,或者不希望将这 4 种方法添加到我的代码中
这是我的代码:
我曾经在
之后将下面的代码插入到上面的代码中NSString *str = [[NSString alloc] initWithData:returnData
encoding:NSUTF8StringEncoding];
NSError * error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:str
options: NSJSONReadingMutableContainers
error: &error];
NSLog(@"%@",json);
但它 work.if 对我的第 3 个问题没有任何有用的答案对 me.i 我 ios.help 我 out.Thanks 的新手非常有用 out.Thanks !
数据很可能是 JSON 而不是纯文本?所以将响应直接转换为 NSDictionary
:
// show loading indicators when you are fetching data
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// hide loading indicators when you received a response
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (connectionError) {
// Handle error
} else {
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"jsonResults: %@", jsonResults);
// reload your views
// [self.myCollectionView reloadData];
}
}];
要制作包含 3 个部分的 collectionView,您需要一个包含 3 个部分的 NSArray,这 3 个数组中的每一个都将包含 12-20 个字典,其中的键和值将填充每个单元格。
NSArray *sectionOne = @[@{@"Section":@"1", @"Object":@"1"}, @{@"Section":@"1", @"Object":@"2"}];
NSArray *sectionTwo = @[@{@"Section":@"2", @"Object":@"1"}, @{@"Section":@"2", @"Object":@"2"}];
NSArray *sectionThree = @[@{@"Section":@"3", @"Object":@"1"}, @{@"Section":@"3", @"Object":@"2"}];
NSArray *collectionData = @[sectionOne, sectionTwo, sectionThree];
NSLog(@"collectionData: %@", collectionData);