2013-01-31 44 views
-1

我想在collectionview中創建不同大小的單元格。在collectionViewLayout sizeForItemAtIndexPath我創建了一個數組與CGSize對象。當我想要檢索存儲在NSArray中(與NSMutableArray的相同)的對象,我得到以下語義問題:使用gcsize對象訪問數組

Returning 'id' from a function with incompatible result type 'CGSize' (aka 'struct CGSize') 

如何訪問我CGSize對象數組中?

編輯:我發現存儲在數組中的值來自NSSize類型。

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    UIImage *image; 
    int row = [indexPath row]; 


    NSArray *mElements = [NSArray arrayWithObjects:[NSValue valueWithCGSize:CGSizeMake(306.0, 270.0)], 
           [NSValue valueWithCGSize:CGSizeMake(100.0, 150.0)], 
           [NSValue valueWithCGSize:CGSizeMake(200.0, 150.0)], 
           [NSValue valueWithCGSize:CGSizeMake(200.0, 150.0)], 
           [NSValue valueWithCGSize:CGSizeMake(100.0, 150.0)], 
           nil]; 

    return [mElements objectAtIndex:row]; // Semantic issue 

} 

我不明白的事情是,在另一部分相同的方法工作...

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    CollectionViewCell *myCell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:Cellid forIndexPath:indexPath]; 

    int row = [indexPath row]; 

    myCell.cellImageView.image = [self.searches objectAtIndex:row]; // Here it works... 
    return myCell; 
} 

爲什麼它與UIImages而不是與CGSize對象的數組,數組的工作?

歡呼聲 - jerik

回答

2

爲什麼它與UIImages一個數組,而不是與CGSize對象的數組工作?

因爲UIImage小號對象,CGSize小號不是對象。但是你可以和應該各自文檔中看着這件事,真的...

這將是非常不夠的,如果你想了解的錯誤消息。您的數組包含對象(類型爲NSValue *),而不是CGSize結構。所以你不能直接從數組中返回一個對象。你必須採取的NSValue對象並提取CGSize結構如此:

return [[mElements objectAtIndex:row] CGSizeValue]; 
+0

嘗試你的代碼,拋出一個SIGABRT。我想用'NSLog(@「size:%@」,[[magazinElements objectAtIndex:row] CGSizeValue]);'得到這個問題信息:'Format指定類型'id',但參數的類型爲' CGSize'(又名'struct CGSize')' – jerik

+0

@jerik不是我的錯。那麼你做了一些不一致的事情。此代碼是正確的。 – 2013-01-31 22:46:16

+0

我只是做了上面的代碼... – jerik

0

SIGABRT被拋出,因爲我的迭代陣比定義mElements陣列更長。現在它可以工作。