alvarolopez
alvarolopez

Reputation: 467

Show check marked rows from UITableView on viewDidLoad

I have a selection list that allows users to select rows. After a row is selected, a checkmark is shown and the cell.textLabel.text value is saved as an array on NSUserDefaults.

What I am having trouble on, is when the user comes back to make the accessoryView checkmark from NSUserDefaults (from previous occasions).

Here is the current code:

if ([[userDefaults objectForKey:@"selectedProducts"]) isEqualTo:cell.textLabel.text] {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

However, I get an error as an invalid approach. Any help is appreciated. Thanks

EDIT: If I print NSLog(@"%@", [userDefaults objectForKey:@"selectedProducts"]) I get the following:

2014-08-16 21:26:01.749 Abonos[575:60b] (
    "Product 1",
    "Product 2",
    "Product 3",
    "Product 4"
)

Upvotes: 0

Views: 87

Answers (2)

Anand Suthar
Anand Suthar

Reputation: 3808

Try this, you should enumerate over all values for every cell

for(uint i=0 ;i<[userDefaults objectForKey:@"selectedProducts”].count ; i++)
{
    if([[[userDefaults objectForKey:@"selectedProducts”]objectAtIndex:i] isEqualToString:cell.textLabel.text])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        break;
    }
}

Upvotes: 2

AdamPro13
AdamPro13

Reputation: 7400

I'm assuming [userDefaults objectForKey:@"selectedProducts"] returns an array. However, you're trying to compare that array that you returned to an NSString, which would never evaluate to true. You need to get the array and iterate through its values, comparing each to the cell text value using isEqualToString: (not isEqualTo).

Upvotes: 0

Related Questions