2013-01-09 29 views
0

我使用UICollectionView和我的應用程序崩潰,因爲我有我的list項目奇數,但我需要拖動部分中的項目。奇數numberOf項目在UICollectionVIew

這是我在每節的項目numbet:

(NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section 
{ 
    return 2; 
} 

這裏的問題是: 在我的名單我有3個項目,當objectAtIndex爲3應用程序崩潰

MSCoupon *coupon = [list objectAtIndex:indexPath.section * 2 + indexPath.row]; 

您有任何解決方案嗎?

+0

什麼崩潰日誌,你會得到什麼?另外,當它崩潰等索引路徑(行和部分)的值是什麼... – Fogmeister

+0

由於未捕獲異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引3超越界限[0 .. 2]' 請告訴我有沒有辦法,即使我提到,在一個部分將是2項,我可以只有一個? – ElizaS

回答

0

我的解決方案如下:

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section 
{ 
    //return 2; 
    return [list count]; 
} 

和:

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView 
{ 
    //return [list count]/2; 
    return 1; 
} 

你對我有更好的建議嗎?

+0

這是最好的。我不認爲我有更好的建議 –

1

這是因爲收集視圖試圖訪問第二部分的第二個元素。這導致崩潰,因爲你的式創建索引3

對於節段的第二元件你式

indexPath.section * 2 + indexPath.row 

給出

1*2 + 1 = 3 

由於您的陣列具有3元素只它將通過異常時你嘗試訪問第四個元素(數組索引從0開始)。 在集合視圖委託你寫這個公式。留下你的代碼的其餘部分解開它應該工作

(NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 
    return (list.count/(section + 1)) >= 2? 2: 1; 
} 
+0

蘇尼爾你好, 你有我的解決方案嗎? 現在它的工作原理因爲我試圖此解決方案: - (NSInteger的)的CollectionView:(UICollectionView *)視圖numberOfItemsInSection:(NSInteger的)部分 { //返回2; return [list count]; } - (NSInteger)numberOfSectionsInCollectionView :(UICollectionView *)collectionView { // return [list count]/2; return 1; } 但這不是一個好的解決方案... – ElizaS

+0

你好艾麗莎請檢查我的編輯。它應該工作 –

+0

我更正了一些語法,請再次檢查 –

相關問題