My app has a TableView with a list of blog articles, sorted by day. When an article’s row is selected, it adds the title of the article into a NSUserDefault NSMutableArray. When the view is refreshed, the app takes the array, uses NSPredicate to sort the array for items in the array that match the title of the article, and if a title matches, it adds a checkmark. The issue is that because I am using NSString it only adds a checkmark to one of the rows…whichever the most recent entry to the NSMutableArray was. Here is my code for cellForRowAtIndexPath:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
NSArray *rowsarray = [defaults objectForKey:@"checkedrows"];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF contains[cd] %@", entry.date];
NSArray *filteredArray = [rowsarray filteredArrayUsingPredicate: predicate];
NSString *myString = [filteredArray componentsJoinedByString:@""];
NSLog(@"The array %@", filteredArray);
NSLog(@"The string %@", myString);
if([entry.date isEqualToString:myString])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
In the console, MYSTRING gets printed out multiple times, once for each item in the array, but only adds a checkmark to the row for the first returned article title. How can I get it to add checkmarks to every one that is in the array?
UPDATE: I added the logs above to print out the filteredArray and then the string. I found the error, it seems to be taking the first item in the array and formatting it in a way that matches the entry.date, but the 2nd is not. Here is the printout in console.
The array (
"Day 2: "
)
The string Day 2:
The array (
(
"Day 1: "
)
)
The string (
"Day 1: "
)
As you can see, the console shows something different for day 1 than day 2. day 2 works, but the other doesn’t. Any suggestions? Oh, also, the original Array before using NSPredicate shows
(
(
"Day 1: "
),
"Day 2: "
)
Could it be I need to edit either the predicate or the string in componentsJoinedByString?
Update:
Code for adding values to NSUserDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *rowsarray2 = [[NSMutableArray alloc] initWithObjects:[defaults objectForKey:@"checkedrows"], nil];
[rowsarray2 addObject:entry.date];
[defaults setObject:rowsarray2 forKey:@"checkedrows"];
[defaults synchronize];
As you have printed, the data structure is not same in both cases. The original array itself shows that first element is an array with another array of string and second element is just a string. So while filtering and joining, you are getting
( "Day 1: " )which represents an array where as you are expecting a string as"Day 1: ". Please check why your original array is having this kind of data structure.In order for your code to work, instead of
It should be,
So you need to change the way, you are adding objects to
NSUserDefaults.Instead of,
Try this,
Basically you were creating an array with previous array as an object instead of adding a new object to that same array.