/* You should save array of selected row indexes to user default like this */
// if first time, create arrSelectedRows array
NSMutableArray * arrSelectedRows = [NSMutableArray new];
// is not first time, load array from userDefaults
arrSelectedRows = [userDefaults objectForKey:@"sight_selectedRow"];
// when user selects row at indexPath
NSNumber * index = [NSNumber numberWithInt:indexPath.row]];
if (arrSelectedRows containsObject:index]) {
[arrSelectedRows removeObject:index];
}
else {
[arrSelectedRows addObject:[NSNumber numberWithInt:index]] ;
}
[userDefaults setObject:arrSelectedRows forKey:@"sight_selectedRow"];
[userDefaults synchronize];
iOS/Objective-C : Remove Array Item in UserDefault
-
17-07-2023 - |
Question
I have a problem with removing an item from an array which is saved in the user defaults.
Example: I have a list in a table with 8 Names. When I select a row, it will be stored in the userdefault, because when I continue and go back it should stay selected. This works fine, but when I deselect the row, it should be removed from the UserDefaults. How can I do that?
I store the selected rows in this way:
[selectedRow insertObject:[NSNumber numberWithInt:indexPath.row] atIndex:arrayIndex];
[userDefaults setObject:selectedRow forKey:@"sight_selectedRow"];
The arrayIndex variable increments everytime when I select a row. The Array has a capacity of 8.
I thought something like: when I press the 4th row, check the UserDefault array items, is there a 4? If so, delete this item. But I fail to delete THIS item.
Thanks :)
P.S. I use Xcode 5
Solution
OTHER TIPS
try this, this works for me. make BOOL property isSelected for each cell if isSelected == YES it's add item to NSUserDefaults, otherwise NO.
in your method tableView:didSelectRowAtIndexPath:
type this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//your code
isSelected = !(isSelected);
}
In place of insertObject, you can use removeIndex property of array for this.
I would suggest you always maintain an array of 8 objects and signal that a selection is invalid using -1
.
Therefore when you first start, and there are no selections, the array is:
@[ @(-1), @(-1), @(-1), @(-1), @(-1), @(-1), @(-1), @(-1) ]
When updating the array, you replace (instead of inserting) the selection using:
[selectedRow replaceObjectAtIndex:arrayIndex
withObject:@(indexPath.row])];
and, of course, any single change to the array requires the entire array to be written to user defaults:
[userDefaults setObject:selectedRow
forKey:@"sight_selectedRow"];
(which you are already doing).
I think you will find this approach to be simpler in the long run as you don't need to worry about the array overflowing your limit of 8 selections.
Try this:
NSMutableArray *tempArray = (NSMutableArray*)[userDefaults objectForKey:@"sight_selectedRow"];;
[tempArray removeObject:unselectedRow];
[userDefaults setObject:tempArray forKey:@"sight_selectedRow"];
I don`t really understand the need to save this in a separate array if you already have your Array.Add a global int value that will keep the row of the chekedItem, at the begining loop through the array or use if else statement or switch statement because you have just 8 items, and see if something is selected if not set chekedItem to 0 (first item in your array)...on didSelectRowAtIndexPath set the chekedItem = indexPath.row. When saving, save the item in array under the chekedItem index.
at the beginning:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *unit = [userDefaults objectForKey:@"unit"];
if ([unit isEqualToString:@"Celsius"])
{
checkedCellIndex = 0;
}
else
{
checkedCellIndex = 1;
} // i have just 2 elements in my array
in the didSelectRowAtIndexPath:
checkedCellIndex = indexPath.row;
[tableViewUnits reloadData];
on saving
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
switch (checkedCellIndex) {
case 0:
[userDefaults setObject:@"Celsius" forKey:@"unit"];
break;
case 1:
[userDefaults setObject:@"Fahrenheit" forKey:@"unit"];
break;
default:
break;