簡體   English   中英

UICollectionViewData中的斷言失敗indexPathForItemAtGlobalIndex

[英]Assertion Failure in UICollectionViewData indexPathForItemAtGlobalIndex

我正在使用performBatchUpdates()來更新我的集合視圖,我正在進行完全刷新,即刪除其中的任何內容並重新插入所有內容。 批量更新是作為Observer的一部分完成的,它附加到NSMutableArraybingDataItems )。

cellItems是包含要或將要插入到集合視圖中的項的數組。

這是代碼:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    cultARunner *_cultARunner = [cultARunner getInstance];
    if ( [[_cultARunner bingDataItems] count] ) {
        [self.collectionView reloadData];
        [[self collectionView] performBatchUpdates: ^{

            int itemSize = [cellItems count];
            NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];

            // first delete the old stuff
            if (itemSize == 0) {
                [arrayWithIndexPaths addObject: [NSIndexPath indexPathForRow: 0 inSection: 0]];
            }
            else {
                for( int i = 0; i < cellItems.count; i++ ) {
                    [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
                }
            }
            [cellItems removeAllObjects];
            if(itemSize) {
                [self.collectionView deleteItemsAtIndexPaths:arrayWithIndexPaths];
            }

            // insert the new stuff
            arrayWithIndexPaths = [NSMutableArray array];
            cellItems = [_cultARunner bingDataItems];
            if ([cellItems count] == 0) {
                [arrayWithIndexPaths addObject: [NSIndexPath indexPathForRow: 0 inSection: 0]];
            }
            else {              
                for( int i = 0; i < [cellItems count]; i++ ) {
                    [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
                }
            }
            [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
        }
        completion:nil];
    }
}

我得到這個錯誤,但不是所有的時間(為什么?)

2012-12-16 13:17:59.789 [16807:19703] *** Assertion failure in -[UICollectionViewData indexPathForItemAtGlobalIndex:], /SourceCache/UIKit_Sim/UIKit-2372/UICollectionViewData.m:442
2012-12-16 13:17:59.790 [16807:19703] DEBUG:    request for index path for global index 1342177227 when there are only 53 items in the collection view

我在這里檢查了唯一提到相同問題的線程: UICollectionView斷言失敗 ,但不是很清楚,即在performBatchUpdates()塊中不建議執行[collectionview reloadData]

關於這里可能出現什么問題的任何建議?

最后! 好的,這就是導致我崩潰的原因。

如前所述,我正在創建補充視圖,以便為我的集合視圖提供自定義樣式的節標題。

問題是:看起來補充視圖的indexPath必須對應於集合中現存單元的indexPath。 如果補充視圖的索引路徑沒有對應的普通單元,則應用程序將崩潰。 我相信在更新過程中,集合視圖會出於某種原因嘗試檢索補充視圖單元格的信息。 它找不到時會崩潰。

希望這也能解決你的問題!

這是此崩潰的正確解決方法:

每個補充視圖都與某個索引路徑相關聯。 如果在索引路徑中沒有單元格(初始加載,您已刪除行等),請通過布局的委托為補充視圖返回高度0。

因此,對於流布局,請實現UICollectionViewDelegateFlowLayout

(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

方法(以及相應的頁腳方法,如果您使用頁腳)具有以下邏輯

if ( you-have-a-cell-at-the-row-for-this-section )
    return myNormalHeaderSize;
else return CGSizeMake( 0,0 );

希望這可以幫助!

reloadData對我不起作用,因為使用performBatchUpdates的整個目的是使動畫更改。 如果使用reloadData ,則只刷新數據,但不使用動畫。

所以“使用reloadData替換performBatchUpdates ”的建議幾乎就是說“放棄你想要做的事情。”

對不起,我只是感到沮喪,因為這個錯誤不斷出現,而我正在嘗試做一些很棒的動畫更新,我的模型是100%正確的,這是一些iOS魔法內部破壞,迫使我改變我的解決方案完全

我的觀點是,收集視圖仍然是錯誤的,不能做復雜的動畫刷新,即使他們應該能夠。 因為對於表視圖來說這曾經是相同的東西,但現在它們非常穩定(雖然需要時間)。

//編輯(2013年9月1日)報告的錯誤現在已經關閉,所以Apple已經解決了這個問題。

我一直有同樣的問題。

我嘗試了很多變體,但最后一個似乎有效的是[self.collectionView reloadData] ,其中"self.collectionView"是集合視圖的名稱。

我已經嘗試了以下方法,直接來自“UICollectionView類參考”:插入,移動和刪除項目。

首先使用它們將物品從一個部分“移動”到另一個部分。

  • deleteItemsAtIndexPaths:

  • insertItemsAtIndexPaths:

接下來,我嘗試了moveItemAtIndexPath:toIndexPath: .

它們都產生了以下錯誤:

斷言失敗 - [UICollectionViewData indexPathForItemAtGlobalIndex:],/ SourceCache / UIKit_Sim / UIKit-2372 / UICollectionViewData.m:442

所以,嘗試“reloadData”方法。

如果從包含頁眉/頁腳的部分中刪除最后一個單元格,則會出現錯誤。

我試圖在那時返回nil for header / footer size / element,這有時會解決問題。

選項:

  1. 重新加載整個表視圖,而不是動畫刪除最后一項。
  2. 添加一個大小小於1的其他不可見的基本單元格。

可能導致此錯誤的cheeseball錯誤是在同一個viewcontroller上的多個collectionView上重用相同的UICollectionViewFlowLayout! 只需為每個集合視圖初始化不同的flowLayouts,你就會好起來!

當我從集合視圖中刪除其中一個單元格時遇到了這個問題。

問題是我使用自定義布局,並且調用layoutAttributesForElementsInRect返回的內容超過了刪除后集合視圖中的單元格數。

顯然,UICollectionView只是遍歷方法返回的數組,而不檢查單元格的數量。

修改方法以返回相同數量的布局屬性解決了崩潰問題。

我仍然無法弄清楚全局索引是如何增加的,但我通過在底層數據源數組中插入臨時項(即cellItems並在viewDidLoad()調用[self.collectionview reloadData]來解決我的問題。

這會在performBatchUpdates()視圖中臨時插入占位符單元格,直到我使用performBatchUpdates()觸發實際進程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM