Stack Asker
Stack Asker

Reputation: 35

How to set Default Object at indexPath?

I want to save data to a specific index in my table.

Here's what I want to achieve theoretically:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:cell.label.text forKey:indexPath];

What is the correct way of doing this?

Upvotes: 0

Views: 235

Answers (3)

Vidhyanand
Vidhyanand

Reputation: 5369

You can modify code as below

NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:cell.label.text forKey:[NSString stringWithFormat:@"$$%d",indexPath.row]]; // modify as per your requirement
[userdefaults synchronize]; 

For Retrieving you can as below.

NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];

cell.textLabel.text=[userdefaults valueForKey:[NSString stringWithFormat:@"$$%d",indexPath.row]]; //modify as per your requirement

Hope it helps you...!

Upvotes: 0

avismara
avismara

Reputation: 5149

You can save the all the data that is relevant to the tableview in an array and store this array in standard user defaults.

EDIT

You CAN use NSUserDefaults for storing other objects than it originially supports. Follow this question here: How to save custom objects in array and store it in NSUserDefaults - iPhone

Upvotes: 0

Rajesh
Rajesh

Reputation: 10434

Try this

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:cell.label.text forKey:[indexPath description]];

Upvotes: 1

Related Questions