How to pass in section value while storing indexPath in NSMutableArray - ios

I am trying to store indexPath in NSMutableArray
[checkBoxIndexArray addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:?]];
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
cell.checkBox.image = [UIImage imageNamed:#"check"];
[self.collectionView insertItemsAtIndexPaths:checkBoxIndexArray];
}
but i don't know on what value shall i put for inSection. I tried entering 0,1 but still it's not working. I am getting following error
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (2) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).
i am getting this error for following code
[self.collectionView insertItemsAtIndexPaths:checkBoxIndexArray];
Please tell me what value shall i put for inSection.

you should use
[checkBoxIndexArray addObject: indexPath];

NSIndexPath *indexPath=[Your_Table_Name indexPathForSelectedRow];
[checkBoxIndexArray addObject: indexPath];

Doing
[checkBoxIndexArray addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:?]]
is same as doing this
[checkBoxIndexArray addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
OR Doing this
[checkBoxIndexArray addObject:indexPath]
Add the indexPath directly, and later on access the rows or section values as you please.

Related

tableview refresh crush Thread1:signal SIGABRT

My app is a IM application, when the application into the background and once again back to the foreground will crush.
This is my part of the code。
-(void)uiAddChatroomMessages:(NSArray*)messages{
NSMutableArray *indexPaths = [[NSMutableArray alloc]init];
int i = (short)self.messageArray.count ;
for (RTIMMessage *msg in messages) {
[self.messageArray addObject:msg];
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:indexPath];
[self.chatMessageTableView beginUpdates];
[self.chatMessageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.chatMessageTableView endUpdates];
[self.chatMessageTableView reloadData];
}
Run to this code "[self.chatMessageTableView endUpdates]",It crush and prompt "Thread1:signal SIGABRT".
2016-08-24 15:49:18.500 RTIM_iOS_Demo[1834:1326398] * Assertion
failure in -[UITableView _endCellAnimationsWithContext:],
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UITableView.m:1716
2016-08-24 15:49:18.590 RTIM_iOS_Demo[1834:1326398] * Terminating
app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Invalid update: invalid number of rows in section 0. The
number of rows contained in an existing section after the update (62)
must be equal to the number of rows contained in that section before
the update (57), plus or minus the number of rows inserted or deleted
from that section (1 inserted, 0 deleted) and plus or minus the number
of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack: (0x18238adb0 0x1819eff80 0x18238ac80 0x182d10154 0x1876e1808 0x100072ccc 0x1000728b0 0x100095244
0x10009468c 0x100077d20 0x100241a7c 0x100241a3c 0x1002474e4
0x182340d50 0x18233ebb8 0x182268c50 0x183b50088 0x187552088
0x1000a5340 0x181e068b8) libc++abi.dylib: terminating with uncaught
exception of type NSException
In your method you added to self.messageArray some new objects but added just one row. Modify your code to.
-(void)uiAddChatroomMessages:(NSArray*)messages{
NSMutableArray *indexPaths = [NSMutableArray array];
NSUInteger i = self.messageArray.count;
for (RTIMMessage *msg in messages) {
[self.messageArray addObject:msg];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:indexPath];
i ++;
}
[self.chatMessageTableView beginUpdates];
[self.chatMessageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.chatMessageTableView endUpdates];
[self.chatMessageTableView reloadData];
}
As per the stack trace it seems like your index path is wrong. You are using array count as row index, try with (array count-1), hope it will work.
After inserting you the number returned by numberOfRowsInSection must match with the new expected value. You might be returning a constant or a number of elements of an array which hasn't changed.

can't delete cell in searchResultsTableView

I can't delete cell in the resultant table in normal everything is OK and after search gives out an error
Assertion failure in -[UISearchResultsTableView
_endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.29.5/UITableView.m:1720
2015-12-14 09:29:04.907 Unirenter[552:178776] *** Terminating app due
to uncaught exception 'NSInternalInconsistencyException', reason:
'Invalid update: invalid number of rows in section 0. The number of
rows contained in an existing section after the update (1) must be
equal to the number of rows contained in that section before the
update (1), plus or minus the number of rows inserted or deleted from
that section (0 inserted, 1 deleted) and plus or minus the number of
rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack: (0x220e185b 0x337a2dff 0x220e1731 0x22e72ddb 0x26408269 0x264218c9 0x264217a3 0x8ab6b 0x26555d97
0x26692ba5 0x2624d5e1 0x2624d571 0x26235487 0x2624cec1 0x26206fe7
0x26245c89 0x2624543b 0x262162f1 0x26214937 0x220a47c7 0x220a43b7
0x220a271f 0x21ff50d9 0x21ff4ecd 0x2b36aaf9 0x2627e2dd 0x8f031
0x33ef0873) libc++abi.dylib: terminating with uncaught exception of
type NSException
here a code of deleting
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
if (tableView == self.tableView) {
[self deleteMessage:[_id_message objectAtIndex:indexPath.row]];
[_id_message removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
NSLog(#"Удаляем id -%d ",[self TitleWithId:[_filteredCandyArray objectAtIndex:indexPath.row]]);
// [_id_message removeObjectAtIndex:[self TitleWithId:[_filteredCandyArray objectAtIndex:indexPath.row]]];
[self.searchDisplayController.searchResultsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
This code is in a method tableView editActionsForRowAtIndexPath
I understood what business. In the simple table I deleted at first an element and then updated and in the retrieval isn't present. I added a method [_filteredCandyArray removeObjectAtIndex:indexPath.row]; and everything earned
You delete the item from it's data source you send deleteRowsAtIndexPaths:withRowAnimation: to it and it should work fine. The same rules apply as to any tableView, if you modify the data source and want to incrementally update the table view make sure you keep the changes consistent between the data source and the table structure. Thnx

Removing a row from tableview causing crash?

I have a tableview that loading data from array called self.data .
Then i am trying to remove a row, so first i remove it from the data array, then i am trying to remove it from the table view with :
Data *d=[[Data alloc] init];
[d removeObject:ID];
NSLog(#"%ld",editingRow); //the right row to remove
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:editingRow inSection:0];
//update table
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
the crash is this :
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Try below code in TableViewDelegate
[self.tableView beginUpdates];
[d removeObject:ID];
[self.tableView deleteRowsAtIndexPaths:[NSIndexPath indexPathForRow:ID inSection:0]
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];

tableView editing delegate method not working(Invalid update: invalid number of rows in section 0)

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
->#property (nonatomic ,strong) NSMutableArray *name;
- (id)initWithFrame:(CGRect)frame viewName:(NSInteger)viewDecide datasourceName:(NSMutableArray *)nameRef
{
}
when i intialize the above MutableArray in this class
like
self.name=[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3", nil];
then using table view editing delegate method , every thing works fine , when i click the button the table goes in edited mode with deletion and insertion.
but when i initialize the same array with data source which i passed in above method . the data inside the tableview render proprely but when i click the edit button its crash.
note:- i used
for row count-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return[self.name count];
}
and for inserting
[strongSelf.tableViewDataView1 insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil] withRowAnimation:UITableViewRowAnimationLeft];
and for deleting i used
[strongSelf.tableViewDataView1 deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil] withRowAnimation:UITableViewRowAnimationLeft];
:-strongSelf is used because this happening inside a block.
Your are removing the item from the table , not from your array. You have to remove the same from the array so that the count will remain same.

UICollectionView: How to handle updates both before and after view is loaded?

I'm having issues updating a collection view when receiving those updates via KVO.
e.g. for deletion I perform the following:
dispatch_async(dispatch_get_main_queue(), ^{
NSUInteger index = [self.albumObjects indexOfObject:oldObject];
NSIndexPath* indexPath = [NSIndexPath indexPathForItem:index inSection:0];
[self.dataSource removeObjectAtIndex:index];
[self.collectionView deleteItemsAtIndexPaths:#[indexPath]];
}
The above gives me the error:
attempt to delete item 0 from section 0 which only contains 0 items before the update
Ok... so lets remove from the data source afterwards then which then returns the following error:
dispatch_async(dispatch_get_main_queue(), ^{
NSUInteger index = [self.albumObjects indexOfObject:oldObject];
NSIndexPath* indexPath = [NSIndexPath indexPathForItem:index inSection:0];
[self.collectionView deleteItemsAtIndexPaths:#[indexPath]];
[self.dataSource removeObjectAtIndex:index];
}
However I now get the following error instead:
Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).
So it seems I can't win? What am I doing wrong here?
It seems the dispatch_async() causes the exception: when you modify your datasource (which adds your updater block to the end of the main queue), the item is already deleted when you try to update your collectionView.
What dataSource do you use? Is it managed manually? Or the the viewController is observing the datasource?

Resources