简体   繁体   中英

iPhone, how can I store / load one value on a simple table view settings screen?

Heres my cellForRowAtIndexPath where I've loading a value for a row, instead of a value from a simple array and converting it to a row.

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
    CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
        reuseIdentifier:CellIdentifier] autorelease];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [dayArray objectAtIndex:row];
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
NSInteger startRow = [myDefaults integerForKey:kStartRow];
if (startRow == row) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}

Heres where I'm saving the value, notice I'm using kStartRow and the array value each time.

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int newRow = [indexPath row];
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setInteger:newRow forKey:kStartRow]; // Set the tablecell 
[myDefaults setObject:[dayArray objectAtIndex:newRow] forKey:kNSUEndOfMonthDay];
[myDefaults synchronize];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadData];
 [self.navigationController popViewControllerAnimated:YES];
}

Using two values makes is a bit messy now when I want to set a default when the app first runs. How can I manage with one stored value ?

cell.accessoryType = indexPath.row == [dayArray indexOfObject:[[NSUserDefaults standardUserDefaults] objectForKey:kNSUEndOfMonthDay]] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM