Select Saved Checkmarked Rows on viewdidload - ios

I have a settings page with a long list with checkmark accesories. Users can select as many rows as they want and I have them saving in NSUserDefaults. I want their previous selections to be selected next time they open settings so I've tried this:
NSArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"choices"]];
if (array.count !=0) {
NSLog(#"not empt");
for (id obj in array) {
NSIndexPath *path = [NSIndexPath indexPathWithIndex:obj];
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
}
}
But the app crashes with this error every time:
erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.
What am I doing wrong? Thanks

The error is quite explanatory. NSIndexPaths in UIKit require two indexes one for the section and one for the row. It's explained here
Assuming you only have one section in your table view:
for (id obj in array) {
NSIndexPath *path = [NSIndexPath indexPathForRow:obj inSection:0];
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
}

Related

Inserting new object past row 8 the app crashes on scroll with NSMutableArray

I have an app that does all things it is supposed to do with the first 8 rows of a UITableView... Once I add that 9th row (so the tableview must scroll) the app crashes...
I have tried numerous variations, but nothing seems to work... I can add cells to my row, as many times as I like, but once I "fill" that 9th row in the table, the app crashes with
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Here is some code used to generate the info in the row in tableview (tabledata is a NSMutableArray and areaNumber.text comes from a customCell)
-(IBAction)save_area:(id)sender {
{
UITableView *tableView = self.myTable;
NSInteger sections = tableView.numberOfSections;
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (int section = 0; section < sections; section++) {
NSInteger rows = [tableView numberOfRowsInSection:section];
for (int row = 0; row < rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
SodTableCell *cell = [self.myTable cellForRowAtIndexPath:indexPath];//**here, for those cells not in current screen, cell is nil**
[cells addObject:cell];
[tabledata removeObjectAtIndex:indexPath.row];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object_areaNumber = [prefs objectForKey:#"tablerow_area_input_by_user"];
if(object_areaNumber != nil){
[tabledata insertObject:cell.areaNumber.text atIndex:indexPath.row];
}
NSUserDefaults *save1 = [NSUserDefaults standardUserDefaults];
[save1 setObject:self.tabledata forKey:#"tablerow_area_input_by_user"];
[save1 synchronize];
NSLog(#"From save button %#",[save1 valueForKey:#"tablerow_area_input_by_user"]);
}
}
}
}
The table can create row after row after row beyond the 9 cells, hundreds if I wanted, and the scroll works... it is when I populate information into the 9th row and which to save that information entered using the code you see above. Any help, much appreciated...
EDIT
In my tableView cellForRowAtIndexPath:
- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil){
cell = [[SodTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
if(indexPath.row >= tabledata.count && [self isEditing]){
cell.areaNumber.text = #"new";
}else{
cell.areaNumber.delegate = self;
cell.areaNumber.text = [tabledata objectAtIndex:indexPath.row];
NSUserDefaults *save1 = [NSUserDefaults standardUserDefaults];
[save1 setObject:self.tabledata forKey:#"tablerow_area_input_by_user"];
[save1 synchronize];
NSLog(#"From cell configuration %#",[save1 valueForKey:#"tablerow_area_input_by_user"]);
}
return cell;
}
I won't talk about how to use UITableView by a right way, i just show you why you got crash.
The error says that you are trying to insert a nil object to a array. As i guess, the root of problem is the below line.
[tabledata insertObject:cell.areaNumber.text atIndex:indexPath.row];
To solve it, check cell.areaNumber.text before inserting to array.
if(object_areaNumber != nil && cell.areaNumber.text){
[tabledata insertObject:cell.areaNumber.text atIndex:indexPath.row];
}
Solved
I have reviewed all the comments made regarding my misunderstanding of UITableViews and related code...
If you review the following link (to my Vimeo page), you should see some nifty things...
Working Example on Vimeo
I was able to load more than the 8 cells in my original problem (as noted in original question) and was even able to load empty rows that the user can save and fill in later! Everything is saved in NSUserDefaults and can be saved, edited or deleted...
Again, thank you for your help... of course, comments that actually helped me are always more welcome and constructive, than the one's that merely pointed out that I didn't know what I was doing...

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.

Dynamically changing data source causing deleteRowsAtIndexPaths:indexes to crash

Tearing my hair out trying to get this to work. I want to perform [self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];,
More detailed code of how I delete:
int index = (int)[self.messages indexOfObject:self.messageToDelete];
[self.messages removeObject:self.messageToDelete];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
NSArray *indexes = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
This works fine however if I get a push notification (i.e a new message received) whilst deleting the app will crash and display an error like:
Assertion failure in -[UITableView _endCellAnimationsWithContext:],
/SourceCache/UIKit/UIKit-3347.44/UITableView.m:1327
2015-07-04 19:12:48.623 myapp[319:24083] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'attempt to delete row 1 from section 0 which only contains 1 rows
before the update'
I suspect this is because my data source is changing, the size of the array that
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
references while deleting won't be consistent because it was incremented by one when the push notification triggered a refresh. Is there any way I can work around this? Am I correct that deleteRowsAtIndexPaths uses the numberOfRowsInSection method?
So, in order to solve your problem you need to ensure that your data source will not change while some table view animations are in place. I would propose to do the following.
First, create two arrays: messagesToDelete and messagesToInsert. These will hold information about which messages you want to delete/insert.
Second, add a Boolean property updatingTable to your table view data source.
Third, add the following functions:
-(void)updateTableIfPossible {
if (!updatingTable) {
updatingTable = [self updateTableViewWithNewUpdates];
}
}
-(BOOL)updateTableViewWithNewUpdates {
if ((messagesToDelete.count == 0)&&(messagesToInsert.count==0)) {
return false;
}
NSMutableArray *indexPathsForMessagesThatNeedDelete = [[NSMutableArray alloc] init];
NSMutableArray *indexPathsForMessagesThatNeedInsert = [[NSMutableArray alloc] init];
// for deletion you need to use original messages to ensure
// that you get correct index paths if there are multiple rows to delete
NSMutableArray *oldMessages = [self.messages copy];
for (id message in messagesToDelete) {
int index = (int)[self.oldMessages indexOfObject:message];
[self.messages removeObject:message];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[indexPathsForMessagesThatNeedDelete addObject:indexPath];
}
for (id message in messagesToInsert) {
[self.messages insertObject:message atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[indexPathsForMessagesThatNeedInsert addObject:indexPath];
}
[messagesToDelete removeAllObjects];
[messagesToInsert removeAllObjects];
// at this point your messages array contains
// all messages which should be displayed at CURRENT time
// now do the following
[CATransaction begin];
[CATransaction setCompletionBlock:^{
updatingTable = NO;
[self updateTableIfPossible];
}];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPathsForMessagesThatNeedDelete withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:indexPathsForMessagesThatNeedInsert withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[CATransaction commit];
return true;
}
Lastly, you need to have the following code in all functions which want to add/delete rows.
To add message
[self.messagesToInsert addObject:message];
[self updateTableIfPossible];
To delete message
[self.messagesToDelete addObject:message];
[self updateTableIfPossible];
What this code does is ensures stability of your data source. Whenever there is a change you add the messages that need to be inserted/deleted into arrays (messagesToDelete and messagesToDelete). You then call a function updateTableIfPossible which will update the table view's data source (and will animate the change) provided that there is no current animation in progress. If there is an animation in progress it will do nothing at this stage.
However, because we have added a completion
[CATransaction setCompletionBlock:^{
updatingTable = NO;
[self updateTableIfPossible];
}];
at the end of the animations our data source will check if there are any new changes that need to be applied to the table view and, if so, it will update the animation.
This is a much safer way of updating your data source. Please let me know if it works for you.
Delete a Row
int index = (int)[self.messages indexOfObject:self.messageToDelete];
[self.messages removeObject:self.messageToDelete];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
Delete a Section
Note : if your TableView have multiple section than you have to delete whole section when section contain only one row instead of deleting row
int index = (int)[self.messages indexOfObject:self.messageToDelete];
[self.messages removeObject:self.messageToDelete];
NSIndexPath *indexPath = [NSIndexSet indexSetWithIndex:0];
[tableView beginUpdates];
[tableView deleteSections:#[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
I have a few test about your code above. And you cannot do that at the same time the least we can do is something like:
//This will wait for `deleteRowsAtIndexPaths:indexes` before the `setCompletionBlock `
//
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[tableView reloadData];
}];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[CATransaction commit];
In your code whe have:
/*
int index = (int)[self.messages indexOfObject:self.messageToDelete];
[self.messages removeObject:self.messageToDelete];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
NSArray *indexes = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
*/
This is not safe because if if the notification is triggered and for some reason [self.tableView reloadData]; called right before deleteRowsAtIndexPaths that will cause an crash, because the tableView is currently updating the datas then interrupt by the deleteRowsAtIndexPaths:, try this sequence to check:
/*
...
[self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
This will cause a crash...
*/
Hmm.. Back to your code, Let's have a simulation that could cause the crash.. THIS is just an assumption though so this is not 100% sure (99%) only. :)
Let assume self.messageToDelete is equal to nil;
int index = (int)[self.messages indexOfObject: nil];
// since you casted this to (int) with would be 0, so `index = 0`
[self.messages removeObject: nil];
// self.messages will remove nil object which is non existing therefore
// self.messages is not updated/altered/changed
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
// indexPath.row == 0
NSArray *indexes = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView deleteRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
// then you attempted to delete index indexPath at row 0 in section 0
// but the datasource is not updated meaning this will crash..
//
// Same thing will happen if `self.messageToDelete` is not existing in your `self.messages `
My suggestion is check the self.messageToDelete first:
if ([self.messages containsObject:self.messageToDelete])
{
// your delete code...
}
Hope this is helpful, Cheers! ;)
'attempt to delete row 1 from section 0 which only contains 1 rows before the update'
This says your index path, during deletion, references row 1. However row 1 doesn't exist. Only row 0 exists (like the section, they are zero-based).
So how are you getting an indexPath one greater than the number of elements?
Can we see your notification handler that inserts a row? You could do some hack where if the animation is in process, you performSelector:withDelay: during the notification processing to allow time for the animation to complete.
In you code, the reason for crash is : you are updating array but not update data source of UITableView. so your dataSource also needs to reflect the changes by the time endUpdates is called.
So as per Apple Documentation.
ManageInsertDeleteRow
To animate a batch insertion, deletion, and reloading of rows and sections, call the corresponding methods within an animation block defined by successive calls to beginUpdates and endUpdates. If you don’t call the insertion, deletion, and reloading methods within this block, row and section indexes may be invalid. Calls to beginUpdates and endUpdates can be nested; all indexes are treated as if there were only the outer update block.
At the conclusion of a block—that is, after endUpdates returns—the table view queries its data source and delegate as usual for row and section data. Thus the collection objects backing the table view should be updated to reflect the new or removed rows or sections.
Example:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
Hope this help you.
I think the answer is alluded to in many of the comments here, but I'll try and tease it out a bit.
The first problem is that it doesn't look like you are bracketing your deletion method call in the beginUpdates/endUpdates methods. This is the first thing I would fix, otherwise as warned in Apple's documentation things can go wrong. (That doesn't mean they will go wrong, you're just safer doing it this way.)
Once you've done that, the important thing is that calling endUpdates checks the data source to make sure that any insertions or deletions are accounted for by calling numberOfRows. For example, if you delete a row (as you are doing), when you call endUpdates you best be sure that you've also deleted an item from the data source. It looks like you are doing that part right because you are removing an item from self.messages, which I assume is your data source.
(Incidentally you are correct that calling deleteRows... on its own without bracketing in beginUpdates/endUpdates also calls numberOfRows on the data source, which you can easily check by putting a break point in it. But again, don't do this, always use beginUpdates/endUpdates.)
Now it is possible that between calling deleteRows... and endUpdates someone else is modifying the self.messages array by adding an object to it, but I don't know if I really believe that because it would have to be extremely unfortunately timed. It's far more likely that when you receive a push message you aren't handling the insertion of the row properly, again by not using beginUpdates/endUpdates, or by doing something else wrong. It would be helpful if you could post the part of the code that handles the insertion.
If that part looks OK then it does look like you are very unfortunately making changes to the self.messages array on a different thread while the deletion code is being called. You can check this by adding a log line where your insertion code adds a message to the array:
NSLog(#"Running on %# thread", [NSThread currentThread]);
or just putting a break point in it and seeing which thread you end up on. If that is indeed the problem, you can dispatch the code that modifies the array and inserts the row onto the main thread (where it should be anyway since it's a UI operation) by doing something like below:
dispatch_async(dispatch_get_main_queue(), ^{
[self.messages addObject:newMessage];
[self.tableView beginUpdates];
[self.tableView insertRows...]
[self.tableView endUpdates];
});
That will ensure that the messages array won't get mutated by another thread while the main thread is busy deleting the row.

UICollectionView: removing cell issue

I am trying to remove a cell from my collectionView, however. When I remove the object from my datasource and attempt a batchupdate, it says the cell doesn't exist anymore.:
'NSInternalInconsistencyException', reason: 'attempt to delete item 0 from section 0 which only contains 0 items before the update'
Before this code I remove the content from my core data, and the line [[usermanager getSelectedUser]loadCards]; actually reloads the datasource containing the content for the cells by getting them from the Core Data.
- (void)cardRemoved:(NSNotification *)note {
NSDictionary *args = [note userInfo];
Card *card = [args objectForKey:#"card"];
[[usermanager getSelectedUser]loadCards];
[self.collectionView performBatchUpdates:^{
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:card.position.intValue inSection:0];
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished){
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
[self.collectionView reloadData];
}];
}
If I print out the amount of Cells before I call the loadCards line, I get the correct amount of rows(As expected).
EDIT
This is what loadCards calls:
-(NSMutableArray *)getCards{
UserModel *selectedUser = [self getSelectedUserFromDB];
NSMutableArray *cards = [[NSMutableArray alloc] init];
for(CardModel *cardModel in selectedUser.cards){
[cards addObject:[self modelToCard:cardModel]];
}
return cards;
}
I noticed, even if I don't call the loadCards method, It says there are no items in the view.
Can anyone help me out? Thank you
Remove the cells from the UICollectionView, then remove them from the model. The same thing applies to UITableView. You also don't need to reload the collection view after removing items.
If you prefer you can just remove items from the model and then reload the collection view and the items that aren't in the model will disappear from the collection view, but without the same animation that comes from removing items from a collection view.

select table row don't work, if element out of view

If the app starts, I need to select table row of the last element, that was used.
The name of the last used element I save in plist.
The code I used is:
id plist = [[PlistHandler alloc] init];// FIXME: Dont work with long list, if the element is out of view.
if ([tableData containsObject:[plist readDataForKey:#"selectedFile"]]) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[tableData indexOfObject:[plist readDataForKey:#"selectedFile"]] inSection:0];
[self.fileTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.fileTable didSelectRowAtIndexPath:indexPath];
}
The problem I found is: If the row is out of view ( table is big and the element if lower then view), I get some exception: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: selectedFile)' Maybe someone know the better way to select a row or to work around.
UPDATE:
I debuged a bit and now I know where the issue happen:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
id plist = [[PlistHandler alloc] init];
[plist writeDataToKey:#"path" andData:[NSString stringWithFormat:#"%#/%#", documentsDirectory, cellText]];
[plist writeDataToKey:#"selectedFile" andData:cellText];
}
It's on the last line, because the cellText is nil, so maybe the index is wrong.
Bit why it works with the elements, that are in the view.
Cell is probably not initialized, because it has not be used. This is, because it is not on the screen.
The deeper problem is that you try to get data from the UI. The UI is for displaying data, not for storing and retrieving data. Retrieve the data from your model. Your application should know how to do this, because it knows how to set the cell.
https://developer.apple.com/library/mac/documentation/general/conceptual/devpedia-cocoacore/MVC.html

Resources