我知道還有關於此主題的其他問題,但沒有描述的解決方案工作,所以我會嘗試解釋我的問題,並希望有人可以幫我解決它。收集視圖不填充
我正在使用一個CollectionViewController來顯示一個對象數組。 我認爲調用方法來填充viewDidLoad方法中的數組是正確的方法,但事實並非如此。
總之,我有一些對象,遠程下載,這些對象有一個url屬性,我想創建一個我需要用來填充我的collectionView的URL的數組。
問題是我沒有我的viewDidLoad中可用的對象數組,所以我無法創建我的urls數組並繼續前進。
這是我的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中填充它,或者至少如何在調用三個主要collectionViewController方法之前使用我的objectsArray? 我需要這個,因爲我必須這樣做:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [[self.posterUrls objectAtIndex:section] count];
}
EDIT1:
我用這個方法來填充我的網址數組:
- (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;}
即使加入了
[self.collectionView reloadData];
返回之前,它不會像應該那樣獲取數組。
編輯2: 是的我正在以異步方式填充我的主要對象數組。
- (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