Quindi ho implementato un UIViewController con una tableview, e fondamentalmente si carica come un set di "filtri" per la mia vista uicollection.Salva i segni di spunta accessori su uitableview quando gli utenti caricano la vista più volte
Ora, quando clicco sui segni di spunta nella mia Tableview, si "filtri" le mie cellule di conseguenza, ma ora quando ricarico di nuovo la vista che voglio per visualizzare i più recenti "segni di spunta" Ho usato, o "filtri ".
Ho visto questo essere implementato con NSUserDefaults, ma non sono stato in grado di implementarlo con successo.
Se qualcuno potesse aiutarmi, sarà molto apprezzato.
CODICE
FiltersViewController.m:
#import "FiltersViewController.h"
@interface FiltersViewController()
@property (nonatomic, strong) NSMutableSet *selectedRowObjects;
//@property (nonatomic, strong) NSArray *filters;
@end
@implementation FiltersViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedRowObjects = [NSMutableSet setWithCapacity:10];
}
- (IBAction)filtersSelected:(id)sender {
[self.delegate filtersSelected:self.selectedRowObjects];
}
- (IBAction)cancelFilterSelection:(id)sender {
[self.delegate filterSelectionCancelled];
}
- (NSString *)getKeyForIndex:(int)index
{
return [NSString stringWithFormat:@"KEY%d",index];
}
- (BOOL) getCheckedForIndex:(int)index
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES)
{
return YES;
}
else
{
return NO;
}
}
- (void) checkedCellAtIndex:(int)index
{
BOOL boolChecked = [self getCheckedForIndex:index];
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *obj = cell.textLabel.text;
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedRowObjects removeObject:obj];
}
else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedRowObjects addObject:obj];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
Si sta recuperando e impostando i valori predefiniti dell'utente in metodi a cui non si fa riferimento da nessun'altra parte nel codice. – duci9y
@ duci9y ok potresti spiegarmelo usando il codice? Capisco cosa stai dicendo –
NSAccedi ai valori userDefault (in diversi punti) per vedere se sono ciò che ti aspetti .. –