- (void)deleteCell
{
[self.collectionViewMenu performBatchUpdates:^{
[self.itemsArray removeObjectAtIndex:1];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.collectionViewMenu deleteItemsAtIndexPaths:#[indexPath]];
} completion:^(BOOL finished) { }];
}
Please help to solve this problem, what am I doing wrong? I got this error after trying to delete an item in my collectionView.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: <_UICollectionViewItemKey: 0x8d705e0> Type = SV Kind = UICollectionElementKindSectionHeader IndexPath = <NSIndexPath: 0x8d6f4b0> {length = 2, path = 0 - 0})'
I solve the problem with remove the header and footer size delegates. the problem was in the fact that I do not return a header or footer.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
setObjectForKey: object cannot be nil means that the cell you are trying to delete is possibly already been deleted.
You should try to debug it and see when you call the delete what views at what index paths are actually in your collection view.
You probably need to implement:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
in your dataSource. This did the trick for me.
I just try to call collectionViewLayout.invalidateLayout() before performBatchUpdates. Then make sure the layout implementation are correctly and make sure that you are not returning negative height for cell. For more detail you can visit below GitHub link.
https://github.com/ra1028/DifferenceKit/issues/13
It helped me to fix the error.
Related
I'm working on a collection view to prompt images and I run into an issue. When I try to set an image to my UIImageView (in my cell) It crashes saying NSInvalidArgument here is the log:
-[UICollectionViewCell img]: unrecognized selector sent to instance 0x7ff063f81170
2018-07-10 10:10:50.645635+0200 App[41377:5706851]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[UICollectionViewCell img]:
unrecognized selector sent to instance 0x7ff063f81170'
Here is the part of the code the error:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
cell.img.image = [UIImage imageNamed:#"test"];
return cell;
}
I'm a 100% sure that it is supposed to work because I used the same method for a UITableView and the images were displayed properly.
Thanks for your reply.
UPDATE:
here is a screen of my storyboard if it can help:
According to the error you posted:
-[UICollectionViewCell img]: unrecognized selector sent to instance 0x7ff063f81170
The reason is obvious that the cell instance dequeued from tableview doesn't have the method "img".
I guess maybe the reuseIdentifier is incorrect so that you got wrong class cell instance which does not implement "img" method.
check your reuse identifier and cast your cell to your class
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (*CollectionViewCell)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
cell.img.image = [UIImage imageNamed:#"test"];
return cell;
}
I have a UICollectionView with 1 section. The user can delete cells from the collection, and I use this code for removal:
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:#[[NSIndexPath indexPathForItem:i inSection:0]]];
[self.media removeObjectAtIndex:i];
} completion:nil];
This works fine for every cell except for the last cell in the collection, which crashes the app every time with the error: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]
NSZombies aren't showing me a stack trace so I put a breakpoint on every line in my code that accesses an array but none were hit, and I found that this error is thrown after deleteItemsAtIndexPaths, numberOfSectionsInCollectionView and sizeForItemAtIndexPath, but before numberOfItemsInSection and cellForItemAtIndexPath
What could be causing this crash? How can I debug it?
There are some similar SO posts, but this one from 2 years ago has no answer UICollectionView crash when deleting last item, and this one only solves the problem if you want to delete the whole section: Removing the last Cell in UICollectionView makes a Crash
UPDATE, here are the data source delegate methods that run before the crash:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(floorf(screenWidth/3), 200);
}
Just put the Exceptions Breakpoint and you'll find where exactly it is crashing.
I have a collectionView, where each 5th cell must be different than others.
i have write the following code:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.row + 1) % 5 == 0) {
return CGSizeMake(screenRect.size.width, screenRect.size.height/5);
}
return CGSizeMake(screenRect.size.width/2, screenRect.size.height/2);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if ((indexPath.row + 1) % 5 == 0 ) {
NSLog(#"iAdCellWithIndex:%ld", (long)indexPath.row);
CustomCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
if (!cell)
cell = [[CustomCollectionViewCell alloc] init];
return cell;
}
ImageThumbCell * cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:#"ImageCell" forIndexPath:indexPath];
if (!cell1)
cell1 = [[ImageThumbCell alloc] init];
[cell1 setResultElement:intermediateResults_[indexPath.row]];
return cell1;
}
1-st cycle (first 5 elements) appear well, but then is loading 6-th element I get an exception :
MyApp[1503:888411] -[CustomCollectionViewCell resultElement]: unrecognized selector sent to instance 0x1a9882a0
I dont understand - Why ? the 6-th cell (cell at index 5) must be kind of ImageThumbCell class, not CustomCollectionView class.
can anyone explain this mistake ?
// sorry for bad english. i'm learning, honestly :)
// thanks
The code you posted is calling the setResultElement method, not resultElement. Thus that code shouldn't generate the crash you're seeing.
Your code for creating cells seems reasonable. On every 5th cell you try to dequeue a different type of cell, and if none are available, you alloc/init that other cell type. That makes sense.
My guess is that your bug is somewhere else in your code. Are you trying to read the value of your cell's resultElement property somewhere else?
Please can anyone help me to achieve something like this-
(developing for iPad iOS7) In UIViewController i have UITableView & UICollectionView(like splitViewController).I just want to change image in UICollectionView when i tap on UITableViewCell.(I am using button over tableViewCell and collecting index in NSInteger).
Please help me to achieve this,I am trying from so many days,but can't achieve till now. :(
Here is the code what i have done till now.
In my viewDidLoad:
self.collectionViewImages=[[NSMutableArray alloc]initWithObjects:#"1.jpg",#"2.jpg",#"3.jpg",nil] ;
Then in CollectionViewMethods:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [_collectionViewImages count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell"
forIndexPath:indexPath];
NSString *collectionViewImageName=[self.collectionViewImages objectAtIndex:indexPath.row];
UIImageView * collectionImage = (UIImageView*)[myCell viewWithTag:11];
collectionImage.image =[UIImage imageNamed:collectionViewImageName];
return myCell;
}
Then on ButtonClick(ofTableViewCell)--
(i am collecting tag of each button in NSInteger and using that property in numberOfItemsInSection like this,
-(void)btnTapped:(id)sender{
int tag= [(UIButton *)sender tag];
NSLog(#"Button Pressed%d",tag);
_buttonIndex=tag;
//_buttonIndex is NSInteger property
//If i add object in NSMutableArray and call reloadData my UI hangs here only.
//If I empty my NSMutableArray and add new objects in it and then call reloadData, I am getting NSRangeException.
}
And now i am using this property in numberOfItemsInSection as...
if (_buttonIndex==1){
[collectionImage setImage:[UIImage imageNamed:[_array1 objectAtIndex:indexPath.row]]];
//My _array1 has 4 objects and my collectionViewImages array has 7 objects.
}
i am getting error message as
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
Its crashing because you are trying to get an item from the array at index 4. But there are only indexes: 0,1,2,3.
Double check the contents of array1 on line (if it crashes here):
[collectionImage setImage:[UIImage imageNamed:[_array1 objectAtIndex:indexPath.row]]];
Otherwise find the other place where you call objectAtIndexand check if it crashes there.
I have a NSFetchedResultController with different section.
I have a crash when I try to search using UISearchDisplayController :
*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit/UIKit-2372/UITableViewRowData.m:1630
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x1d2c4120> 2 indexes [0, 1])'
I checked and my search array has indeed two entries (the expected result):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
It returns 1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
It returns 2
Funny thing, if I have only one section, it works perfectly.
Help please! :)
Not sure if you figured it out, I would think you did but assuming you are using
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
in your cellForRowAtIndexPath
do
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
also make sure you are using self.tableView