還是有點新,我遇到了一些問題,希望有人能幫忙。我試圖從我的服務器加載一個JSON字符串到iOS6的collectionview中 我可以使用從viewDidLoad方法調用的fetchedData方法來引入數據,該部分工作正常。在fetchedData方法中,我分解出JSON數據並將它放在NSDictionaries和NSArrays中,並將正確的數據轉儲到日誌中以查看它。以多種方式訪問變量
問題是,當我嘗試使用我的代碼中的任何其他信息的任何信息,如獲取任何hte數組中的元素數量作爲計數器來填充collectionview。
這可能是我累了,但我似乎無法繞過這部分。許多主要變量的聲明在fetchedData方法中,我認爲自從在那裏聲明它可能是我在其他地方看不到它們的原因,所以我將變量的聲明移到了接口部分,並希望這會使變量GLOBAL和fetchedData方法繼續正常工作,但沒有其他地方。
當我在單元格定義區域中放置中斷時,可以在調試器窗口中看到變量爲空。
我不確定代碼中的哪些部分可能需要查看,請通知我,我可以發佈它們,但也許有人可以舉例說明如何使用多種方法訪問數組和字典項目。
爲了避免混淆,並暴露我的代碼大雜燴在這一點上無論如何這裏是.m文件或至少是大部分它請不要撕裂強硬的編碼風格我一直在嘗試任何我能想到的並且自己撕毀了它,並且很晚了。
#import "ICBCollectionViewController.h"
#import "ICBCollectionViewCell.h"
#import "ICBDetailViewController.h"
@interface ICBCollectionViewController() {
NSDictionary* json;
NSDictionary* title;
NSDictionary* shortDescrip;
NSDictionary* longDescrip;
NSDictionary* price;
NSDictionary* path;
NSDictionary* sKU;
NSDictionary* audiotrack;
NSDictionary* audiotracksize;
NSArray* titles;
NSArray* shortDescription;
NSArray* longDescription;
NSArray* prices;
// NSArray* paths;
NSArray* SKUs;
NSArray* audiotracks;
NSArray* audiotracksizes;
}
@end
/*
@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end
@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress
{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
-(NSData*)toJSON
{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
*/
@implementation ICBCollectionViewController
@synthesize paths;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: imobURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
// Do any additional setup after loading the view.
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
//parse out the json data
json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
titles = [json objectForKey:@"title"]; //2
shortDescription = [json objectForKey:@"shortD"];
longDescription = [json objectForKey:@"longD"];
prices = [json objectForKey:@"price"];
self.paths = [json objectForKey:@"path"];
SKUs = [json objectForKey:@"SKU"];
audiotracks = [json objectForKey:@"audiotrack"];
audiotracksizes = [json objectForKey:@"audiotracksize"];
NSLog(@"paths: %@", paths); //3
// NSLog(@"shortDescrip: %@", shortDescription);
NSInteger t=7;
// 1) Get the latest loan
title = [titles objectAtIndex:t];
shortDescrip = [shortDescription objectAtIndex:t];
longDescrip = [longDescription objectAtIndex:t];
price = [prices objectAtIndex:t];
path = [paths objectAtIndex:t];
sKU = [SKUs objectAtIndex:t];
audiotrack = [audiotracks objectAtIndex:t];
audiotracksize = [audiotracksizes objectAtIndex:t];
//NSLog(title.count text);
//NSLog(title.allValues);
// 2) Get the data
NSString* Title = [title objectForKey:@"title"];
NSString* ShortDescrip = [shortDescrip objectForKey:@"shortD"];
NSString* LongDescrip = [longDescrip objectForKey:@"longD"];
NSNumber* Price = [price objectForKey:@"price"];
NSString* Path = [path objectForKey:@"path"];
NSString* SKU = [sKU objectForKey:@"SKU"];
NSString* AudioTrack = [audiotrack objectForKey:@"audiotrack"];
NSNumber* AudioTrackSize = [audiotracksize objectForKey:@"audiotracksize"];
/*************************HERE THE DATA EXISTS*******************************/
/******** Path = "XYXYXYXYXYXY" for example ********************************/
// 3) Set the label appropriately
NSLog([NSString stringWithFormat:@"Here is some data: Title: %@ Path %@ SKU: %@ Price: %@ Track %@ Size %@",Title, Path, SKU, Price, LongDescrip, AudioTrackSize]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//DetailSegue
if ([segue.identifier isEqualToString:@"DetailSegue"]) {
ICBCollectionViewCell *cell = (ICBCollectionViewCell *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
ICBDetailViewController *dvc = (ICBDetailViewController *)[segue destinationViewController];
dvc.img = [UIImage imageNamed:@"MusicPlayerGraphic.png"];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(@"paths qty = %d",[paths count]);
return 20;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *[email protected]"Cell";
ICBCollectionViewCell *cell = (ICBCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// paths = [json objectForKey:@"path"];
NSDictionary* path = [paths objectAtIndex:indexPath.row];
NSString* Path = [path objectForKey:@"path"];
// NSString* Path = [paths objectAtIndex:indexPath.row];
NSLog(@"%d",indexPath.row);
/***********************HERE IT DOES NOT**************************/
/******** Path = "" **********************************************/
NSLog(@"xxx");
NSLog(path);
NSLog(paths);
NSLog(Path);
NSLog(@"ZZZ");
[email protected]"deepsleep";
NSLog(@"xxx");
NSLog(Path);
NSLog(@"ZZZ");
// paths = [json objectForKey:@"path"];
// NSString* Path = [path objectForKey:@"path"];
NSString *imagefile = [NSString stringWithFormat:@"https://imobilize.s3.amazonaws.com/glennharrold/data/%@/mid.png", Path];
NSLog(imagefile);
NSURL *url1=[NSURL URLWithString:imagefile];
dispatch_async(kBgQueue, ^{
NSData *data1 = [NSData dataWithContentsOfURL:url1];
cell.imageView.image =[[UIImage alloc]initWithData:data1];
});
return cell;
}
@end
我說的是在一個單一的.m文件,我將這些代碼添加到我的問題,它包含了所有的方法中的數據你的幫助我提到 –
如果這一切都在宣告。 m文件,那麼它將不可訪問,因爲這些屬性不是公共的。 – AMayes
這是否包括在頂部的相同.m文件中使用它們,我可以向他們展示它們,但是在底部它們已經不存在了,我該如何更改它? –