このトピックに関するその他の質問がありますが、そこに記載されている解決方法はありません。そのため、問題を説明して誰かが解決を助けることを願っています。コレクションビューにデータが入力されていません
私はオブジェクトの配列を表示するためにCollectionViewControllerを使用しています。 私は配列をviewDidLoadメソッドに取り込むメソッドを呼び出すのが正しい方法だと思っていましたが、そうではありません。
要するに、リモートからダウンロードされたオブジェクトにurl属性があり、コレクションビューを作成するために必要なURLの配列を作成したいとします。
問題はviewDidLoadで使用できるオブジェクトの配列がないため、URLの配列を作成してさらに進めることができないことです。
これは私のviewDidLoadメソッドである:
- (void)viewDidLoad {
[super viewDidLoad];
[[self collectionView]setDataSource:self];
[[self collectionView]setDelegate:self];
[self.backgroundImage setImage:[UIImage imageNamed:@"app_background"]];
objectModel = [[ObjectModel alloc] init];
self.availableObjectsArray = [[NSMutableArray alloc] init];
self.posterUrls = [[NSMutableArray alloc] init];
[self createObjectArrayFromUrl:serverUrl intoArray:self.availableObjectsArray];
//FIRST TRY NSMutableArray *postersUrlSection1 = [[NSMutableArray alloc] initWithArray:[self posterUrlsFromObjects:self.availableObjectsArray]];
//SECOND TRY (AFTER ALLOC AND INIT) postersUrlSection1 = [self posterUrlsFromObjects:self.availableMoviesArray];
// [[self collectionView] reloadData]; ADDED AS PART OF THE SOLUTION BUT DIDN'T SOLVE THE ISSUE
NSLog(@"Array OBJECTS populated? %@", self.availableMoviesArray);
NSLog(@"Array URLS populated? %@", postersUrlSection1);
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
// [self.collectionView reloadData]; ADDED AS A SOLUTION BUT NOT SOLVING MY ISSUE
}
私はメインのオブジェクト配列が読み込まれますことをどうにか:それはのviewDidLoad内のログには何も表示されない場合であっても、私がしたい場合は、それが何らかの形でオブジェクトをロードします
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
方法で彼らと何かをするが、明らかに私はすぐに(例えばのviewDidLoadで)それを必要とする場合、それは人口取得していません。 viewDidLoadにそれを設定するにはどうしたらいいですか?または少なくとも3つの主要なcollectionViewControllerメソッドが呼び出される前に、どうやってobjectsArrayを使用できますか?私はこれをしなければならないので、 私はこれを必要とする:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [[self.posterUrls objectAtIndex:section] count];
}
EDIT1:でも
[self.collectionView reloadData];
を追加することにより、
- (NSMutableArray *) posterUrlsFromObjects: (NSMutableArray *) objectsArray{
Object *object = [[object alloc]init];
NSMutableArray *posterUrls = [[NSMutableArray alloc] init];
NSURL *placeholder = [NSURL URLWithString:@"placeholder"];
for (NSInteger index = 0; index < [objectsArray count]; ++index) {
object = [objectsArray objectAtIndex:index];
if (object.posterURL != nil)
[posterUrls addObject:[NSURL URLWithString:object.posterURL]];
else
[posterUrls addObject:placeholder];
}
return posterUrls;}
:
は、私は私のURLアレイを移入するために、このメソッドを使用します返す前に
の配列を取得していない場合、配列の配列は取得されません。
EDIT2: はいメインオブジェクト配列に非同期でデータを取り込みます。
- (void) createObjectArrayFromUrl: (NSString *) url intoArray: (NSMutableArray *) objectArray{
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if(error) {
NSLog(@"Error");
}
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableDictionary *jsonDataDictionary = [objectModel getRemoteJsonDataFromUrl:url];
[movieArray removeAllObjects];
[movieArray addObjectsFromArray:[objectModel arrayFromDictionary:jsonDataDictionary]];
[[self collectionView] reloadData];
});
}];
[task resume];
}
EDIT3:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2; //LOG RETURNS 2
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.availableMoviesArray count]; //LOG RETURNS 0
// WHAT I WANTED TO DO return [[self.posterUrls objectAtIndex:section] count];
}
呼び出さcollectionView:cellForItemAtIndexPath:
方法。
私の推測では、あなたが言ったので、「:それはのviewDidLoad内のログには何も表示されていない場合でも、私は人口の主な目的の配列を取得するために管理」、非同期方法であなたの配列を移入していることです。 'createObjectArrayFromUrl:intoArray:'のコードを表示できますか? – Larme
私はあなたの時間のために、私の質問を編集しました。 – DarthGalm
'createObjectArrayFromUrl:intoArray:'では、 'movieArray'が設定されます。しかし、 'collectionView:numberOfItemsInSection:'で 'posterUrls'を探すので、気にしません。これは単にnoneオブジェクトで初期化されます。 – Larme