오래된 데이터에 대한 UICollectionView 어설 션 오류
내 컬렉션보기에서 한 배치의 이미지를 언로드 한 다음 다른 배치로 바꾸려고하는 과정에서 원본 또는 후속 이미지 그룹이 의도 한 대체 이미지보다 많거나 적은지 여부에 따라 오류가 발생합니다. 다음과 같은 주장 오류가 발생합니다.
*** Assertion failure in -[UICollectionViewData validateLayoutInRect:],
/SourceCache/UIKit_Sim/UIKit-2891.1/UICollectionViewData.m:341
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'UICollectionView recieved layout attributes for a cell with an
index path that does not exist: <NSIndexPath: 0xb141c60> {length = 2, path = 0 - 2}
이 경우 기존 이미지 목록 수는 5 개이고 새 이미지 목록 수는 2 개였습니다. 따라서 세 번째 이미지에 도달했을 때 예외가 발생했습니다. 이는 UI CollectionViewDataDelegate가 데이터 스트림의 변경 사항을 알지 못했음을 나타냅니다. .
UICollectionView에서 새 이미지를 참조하는 방법에 대한 제안 사항이 있습니까? 물론 나는 'reloadData'라고 불렀다…
감사합니다
나는 같은 문제에 처해 있습니다. 코드는 6.1에서 실행되고 7.0에서 충돌합니다. 다음과 같은 방법으로 문제를 해결했습니다.
기능에서
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
나는 전화한다
[myCollectionView.collectionViewLayout invalidateLayout];
그게 다야.
iOS 10 및 11에서는 다음과 같은 이점이 있습니다.
collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()
레이아웃 무효화는 데이터를 다시로드 한 후 여야합니다.
Dominic Sander 와 user1544494 가 모두 옳고 솔루션이 좋습니다.
불행히도 minimumLineSpacingForSectionAtIndex
또는 을 설정하면 minimumInteritemSpacingForSectionAtIndex
collectionView의 모양이 깨질 것입니다 (조만간 또는 나중에).
퍼팅 invalidateLayout
에 viewWillLayoutSubviews
대한 답변이 질문을하고하는 것은 viewCollection의 모양을 유지하는 데 도움이됩니다.
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[viewCollection.collectionViewLayout invalidateLayout];
}
간단 해. 아래 문장처럼.
'UICollectionView recieved layout attributes for a cell with an
index path that does not exist: <NSIndexPath: 0xb141c60> {length = 2, path = 0 - 2}
이는 dataSouce에 indexPath (0,2)가 없음을 의미합니다. 그러나 UICollectionViewLayout은 indexPath (0,2)에 대한 UICollectionViewLayoutAttributes를 반환합니다.
UICollectionViewLayoutAttributes를 반환해야합니다. dataSouce에만 존재합니다.
iOS7에서 바뀌 었다고 생각합니다.
내 문제는 UICollectionViews
하나 안에 두 개가 있다는 것 UIViewController
입니다. 그리고 나는 둘 다 UICollectionViews
동일한 UICollectionViewLayout
하위 클래스에 연결되었습니다 . 나는 각각 UICollectionView
의 UICollectionViewLayout
하위 클래스 를 갖도록 변경 하여이 문제를 해결했습니다 .
출처 : 이 질문
컬렉션 뷰 데이터 소스를 업데이트하여이 충돌을 해결했습니다.
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
[collectionView.collectionViewLayout invalidateLayout];
return collectionArray.count;
}
나는 같은 사고를 당했다.
내 앱에서 문제는 UICollectionViewLayoutAttributes로 배열을 비우지 않았다는 것입니다. 각 셀의 레이아웃 속성을 저장하기 위해 prepareLayout () 메서드에서 사용하고 있습니다.
var itemAttributes: Array<UICollectionViewLayoutAttributes> = Array<UICollectionViewLayoutAttributes>()
단지와 함께 self.itemAttributes.removeAll()
prepareLayout의 첫 번째 줄에서, 그것을 작동합니다.
컬렉션 뷰의 내용을 수정 한 후이 문제가 발생했습니다. 제 경우에 효과가 있었던 해결책은 다시로드 한 후 레이아웃을 무효화하는 것입니다. 다시로드하기 전에 수행하면 작동하지 않습니다.
[collectionView reloadData];
//forces the layout attributes to be recalculated for new data
[collectionView.collectionViewLayout invalidateLayout];
The solution I found was to ensure the indexPath
I was creating in the layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]?
method is valid for the row. Previously, I was using (where i
is my loop counter):
var indexPath = NSIndexPath(index: i)
var attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
But updating it to use the following solved it:
var indexPath = NSIndexPath(forRow: i, inSection: 0)!
var attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
I was able to solve this issue by creating a subclass of UICollectionViewFlowLayout
and overriding this method to return YES
:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
I have encountered with this issue and it was pretty annoying. My fix is to forget about UICollectionViewController
and use instead regular UIViewController
with UICollectionView
inside.
Make sure you update the contentSize
of your collectionViewLayout
. So after getting new images (2, instead of 5), recalculate the contentSize
and set it.
I've had this bug too and found a workaround. For me, the UICollectionView was launching this under iOS 7, working perfectly on iOS 8.
Check this article: What's causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist
In 2 words: Auto-Layout. Disable it on the view containing the UICollectionView and for me, it worked.
The collectionViewLayout caches the layout attributes. In viewwillappear . Create a new instance of collectionViewLayout and assign it to collectionview.collectionViewLayout In this way all the cached attributes will purge before the reload Your problem might be resolved. Worked for me , especially when you are using other collectionViewLayout libraries.
I had similar problem (using Swift2.0, XCode 7).
The app crashed with UICollectionView received layout attributes for a cell with an index path that does not exist
...
In my case, since I used the storyboard, it just turned out that I forgot to connect the IBOutlet that was defined in my viewController with the actual collectionView defined in the storyboard. Connecting the two fixed the problem.
I have figured it out. If you are using nib/xib to organize your UITableViewCell
and nested UICollectionView
,you can avoid this error by overriding this method.
- (void)prepareForReuse {
[super prepareForReuse];
[self.collectionView.collectionViewLayout invalidateLayout];
}
Hope it helps.
It's mean that there is no indexPath(0,2) on dataSouce. But, your UICollectionViewLayout return a UICollectionViewLayoutAttributes for indexPath(0,2). By TopChul
That's Right! For Me, the issue happened because I use same collection layout(instance) for two collectionView! So that layout confused between two collection View.
It work fine after I use different layout between different collection view.
I ran into something similar when trying to duplicate a collection view into another storyboard.
'UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 1 - 0}'
At first, I am looking for a quick fix. Tried copy paste various StackOverflow answers.
But I wrote my own layout class. So I try to debug discreetly, it could be my implementation to blame, right? Found that numberOfSections
method was never called. The collection view assumed that it had only one section.
Then I found view controller class forgot to conform to UICollectionViewDataSource
. Though the dataSource was hooked up in the storyboard but probably the view controller class will be downcast, like if let ds = dataSource as? UICollectionViewDataSource {ds.numberOfSections...}
, which would fail silently.
So I added the conformance to UICollectionViewDataSource
and everything works fine. My guess could be inaccurate. But the lesson is whenever there's a bug you're not familiar with, settle down and understand it. UICollectionView received layout attributes for a cell with an index path that does not exist, it means exactly what it says. Not that hard right? Don't try to find a silver bullet, like many of the answers here. They are all great but your code is the real battle place.
I run into the same problem when I use UICollectionViewFlowLayout
as CollectionView's collectionViewLayout
.
Declare the parent viewController
implemented UICollectionViewDelegateFlowLayout
and assign it as collectionView
's delegate can solve this problem.
In my case i had a custom UICollectionViewFlowLayout
. After removing cells from the collectionView the app crashed. The fix was to removeAll()
previously calculated attributes. So, first line after override func prepare()
is arrayHoldingYourAttributes.removeAll()
.
Hollo, I have the same issue while insert a collectionView into another collectionView and reloadData in main queue. Finally I reloadData before a new data convert to collectionView.
dispatch_async(dispatch_get_main_queue(), ^{
[_collectionView reloadData];
});
_notes = notes;
[_collectionView reloadData];
참고URL : https://stackoverflow.com/questions/18339030/uicollectionview-assertion-error-on-stale-data
'IT박스' 카테고리의 다른 글
Woocommerce에서 맞춤 제품 속성 가져 오기 (0) | 2020.12.03 |
---|---|
터미널에서 현재 분기 및 폴더 경로를 표시하려면 어떻게해야합니까? (0) | 2020.12.03 |
Postman Jetpack에서 테스트 컬렉션 내보내기 (0) | 2020.12.03 |
pm2를 사용하여 앱에 인수를 전달하는 방법은 무엇입니까? (0) | 2020.12.03 |
vs 코드는 '@ angular / core'모듈 또는 다른 모듈을 찾을 수 없습니다. (0) | 2020.12.03 |