How to use NSUserDefaults to store a row property
I have a tab bar based application, with a TableViewController
on a tab.
When i update my app, i add a new row to my Table
.
What开发者_运维技巧 i want to do is store the highlight color of a row of my TableViewController
as soon as i insert a new one, for at least 3 sessions.
I know that i have to use NSUserDefaults
, but, where?
I mean: should i use it into my rowViewController
(which is a UIViewController
) or into my TableViewController
(where i have all the rows)?
And how can i import a cell property into the UIViewController
(if is in there that i have to use the NSUserDefaults
)?
i have something like:
- (void) applicationDidFinishLaunching:(NSNotification*) notice {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:cell];
if ([(NSInteger*)[[NSUserDefaults standardUserDefaults] integerForKey:cell] intValue] < 3) {
// render highlighted...
} else {
// render normal
} }
I can't do it 'cause it's a UIViewController, right? Then in the .h at
@interface rowViewController : UIViewController
should i add something in order to implement the cell and then to set its properties (color)?
Thanks in advance.
You can do it the way you did in your question, but there is a big problem in your code. You use NSInteger like if it was an object, whereas it is an atomic type.
Don't confuse NSInteger
and NSNumber
:
NSNumber
is a class that aims to encapsulate numbers in an Objective-C object so that you can use it whereNSObjects
are requested (like when you want to store them inNSDictionaries
)NSInteger
is an alias (typedef) for an integer type. Use it the same way you would use anint
.
So in your code, NSUserDefaults
's setInteger:forKey:
and integerForKey:
uses NSInteger values (namely atomic values, not objects). No need to use *
and intValue
then to retrieve the real value, you already have it! (The code like you wrote it will surely crash, interpreting an integer value as a pointer to a memory address!)
Moreover, you can't use the cell as the key of your NSUserDefaults
. NSUserDefaults
only accept strings for the keys, and moreover UITableViewCells
are reused (recycled each time you scroll) and not persistant. You should instead build a string that uses the NSIndexPath
of your cell and use it for your NSUserDefaults
key; then store the background color of a cell/row at a given position in the UITableView
(and anyway that's better separation of the Model and the View in the MVC design pattern).
// Build a string from the IndexPath that will be used as a unique key for your userdefaults, to store info on the corresponding row in your tableview
NSString* key = [NSString stringWithFormat:@"bkg_%i:%i",indexPath.section,indexPath.row];
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:key];
if ([[NSUserDefaults standardUserDefaults] integerForKey:key] < 3)
{
// render highlighted...
} else {
// render normal
}
Note that integerForKey:
can be seen as a call to objectForKey:
(that will return an NSObject
, typically an NSNumber
in your case, which is an object encapsulating the integer value) followed by a call to integerValue
.
// These lines...
NSNumber* nbrObj = [[NSUserDefaults standardUserDefaults] objectForKey:@"mykey"];
NSInteger nbr = [nbrObj integerValue];
// are equivalent to the following, shorter line:
NSInteger nbr = [[NSUserDefaults standardUserDefaults] integerForKey:@"mykey"];
精彩评论