我是iOS开发中的新手,我将我的JSON数据解析为我的数据,如
{
mid: "1",
name: "august 2014",
front_image:"http://www.truemanindiamagazine.com/webservice/magazineimage/frontimage/1.jpg",
title: "August fist edition",
release_date: "2014-08-01",
short_description: "Here we present to you for the first time our Magazine Trueman India. “Trueman India” covers Bollywood Gossips, film reviews, upcoming movies, F",
current_issue: 0,
-demopage: [
{
link:my image URL
page_no: "1"
},
{
link: my image URL,
page_no: "16"
},
{
link: my image URL,
page_no: "2"
},
{
link: my image URL,
page_no: "22"
},
{
link: my image URL,
page_no: "25"
},
{
link: my image URL,
page_no: "26"
},
我将此数据解析为两个数组,如
NSError* error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa= nil;
if([jsonDict count]>0)
{
NSArray *arrData = [jsonDict objectForKey:@"data"];
if([arrData count]>0)
{
NSDictionary *imgDict = [arrData objectAtIndex:1];
if([imgDict count]>0)
{
NSMutableArray *imgsLinkArray = [imgDict objectForKey:@"demopage"];
self.imagesa = imgsLinkArray;
}
}
}
然后是Parsed,我得到了我想要的东西但是这里有一个问题就是当我在Scrollview中显示带有页码的图像然后我显示我的图像,因为我得到了JSON数据,但我希望我的Imageview是基于在PageNumber上如从1到数据数组大小。请给我解决方案我根据我的页码排列我的图像这里我的代码和页码解析是
for(int index=0; index < [self.imagesa count]; index++)
{
NSDictionary *dict=[self.imagesa objectAtIndex:index];
NSString *image=[dict valueForKey:@"link"];
NSString *pageNumber=[dict valueForKey:@"page_no"];
smallImage = [[UIImageView alloc] init];
pageLabel=[[UILabel alloc]init];
[pageLabel setBackgroundColor:[UIColor clearColor]];
[smallImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
[pageLabel setText:[NSString stringWithFormat:@"Page%@",pageNumber]];
然后我将我的标签和imageview添加到我的Scrollview中,然后它按照我的意愿工作,但它不是显示图像基于Page nuber就像来自!要达到阵列大小,请给我解决方案。
答案 0 :(得分:2)
以这种方式排序imagesLinkArray
:
self.imagesa = [[imgsLinkArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1[@"page_no"] intValue] > [obj2[@"page_no"] intValue]) {
return NSOrderedDescending;
} else if ([obj1[@"page_no"] intValue] < [obj2[@"page_no"] intValue]) {
return NSOrderedAscending;
}
return NSOrderedSame;
}] mutableCopy];
答案 1 :(得分:0)
您应该根据索引变量(+1)设置pageLabel测试,而不是pageNumber。如下:
[pageLabel setText:[NSString stringWithFormat:@"Page %lu",(long unsigned)(index+1)]];
答案 2 :(得分:0)
在数组中删除值,像这样
if([imgDict count]>0)
{
NSMutableArray *imgsLinkArray = [imgDict objectForKey:@"demopage"];
self.imagesa = imgsLinkArray;
}
然后你拥有self.imagesa数组中的所有值....
所以只是怀疑......
[pageLabel setText:[NSString stringWithFormat:@"Page %lu",(long unsigned)[[self.imagesa objectAtIndex:index] valueForKey:@"page_no"]]];
为你的短阵列OrderBy ............
NSMutableArray *recent=[[NSMutableArray alloc]init];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"page_no" ascending:YES selector:@selector(localizedStandardCompare:)] ;
[self.imagesa sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
recent = [[self.imagesa sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]] copy];
self.imagesa = (NSMutableArray *) [[NSMutableArray alloc]initWithArray:recent];