当用户多次加载视图时,在uitableview上保存附件复选标记

所以我实现了一个带有tableview的UIViewController,基本上它作为我的uicollectionview的一组“filter”加载。

现在,当我点击tableview中的复选标记时,它会相应地“过滤”我的单元格,但现在当我再次重新加载视图时,我想显示我使用过的最新“复选标记”或“filter”。

我已经看到这是用NSUserDefaults实现的,但我无法成功实现它。

如果有人能帮助我,我将不胜感激。

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 

你还需要检查cellForRowAtIndexPath。 在此编写此代码

 if([[NSUserDefaults standardUserDefaults] objectForKey:[self getKeyForIndex:indexPath.row]]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } 

是的,不要忘记在didSelectRowAtIndexPath中调用此方法

 [self checkedCellAtIndex:indexPath.row]; 

请享用。

你做的几乎一切正常。 您只需要将您的逻辑用于读取cellForRowAtIndexPath委托方法中的NSUserDefault值(对于复选框)。 这是在屏幕上显示UITableViewCells时绘制UITableViewCells的方法。 像这样的东西:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row]; if ([self getCheckedForIndex:indexPath.row]) { //code to set checkbox } return cell; } 
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *simpleTableIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } cell.textLabel.text = ArrayOfObject[indexPath.row]; NSUserDefaults *ud =[NSUserDefaults standardUserDefaults]; if([[ud objectForKey:@"selectedObjectKey "]isEqualToString:ArrayOfObject[indexPath.row]]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } 

你完美地实现了。 您只需修改cellForRowAtIndexPath方法即可

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row]; BOOL checked = [self getCheckedForIndex:indexPath.row]; if(checked) cell.accessoryType = UITableViewCellAccessoryCheckmark; else cell.accessoryType = UITableViewCellAccessoryNone; return cell; } 

同时调用[self checkedCellAtIndex:indexPath.row]; 来自UITableView的didSelectRowAtIndexPath方法。

使用NSUserDefaults保存tableView CheckMark的最佳方法

  @interface TableViewController (){ NSArray *TableTitles; NSMutableArray *SelectedRows; } 

viewDidLoad中

  - (void)viewDidLoad { [super viewDidLoad]; // table view array TableTitles = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]; NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:@"SelectedRows"]]; } 

的cellForRowAtIndexPath

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.textLabel.text = TableTitles[indexPath.row]; NSNumber *obj = [NSNumber numberWithInteger:indexPath.row]; if ([SelectedRows containsObject:obj]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } 

didSelectRowAtIndexPath方法

  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSNumber *obj = [NSNumber numberWithInteger:indexPath.row]; if ([SelectedRows containsObject:obj]) { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; [SelectedRows removeObject:obj]; [tableView reloadData]; }else{ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; [SelectedRows addObject:obj]; [tableView reloadData]; } NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; [userDef setObject:SelectedRows forKey:@"SelectedRows"]; [userDef synchronize]; } 

更新 …

我用一种新的方式更新了所有代码,这是保存TableView CheckMarks的最佳方式,现在它既简单又高效,现在只有一个数组用于保存和加载,代码减少了太多:)

您应该在调用cellForRowAtIndexPathNSUserDefaults加载数据

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row]; //You should set accessoryType here by NSUserDefaults data if ([self getCheckedForIndex:indexPath.row]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else{ cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } 

我认为您要查找的是在进入和退出视图时获取filter表的当前状态。 你需要利用- (void)viewWillDisappear:(BOOL)animated将表的当前状态保存到NSUserDefaults,然后使用- (void)viewDidLoad- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath在表重新加载时设置复选标记。

 @interface FiltersViewController () { NSMutableArray *_filterStates; } @end - (void)viewDidLoad { filterStates = [[NSMutableArray alloc] init]; // get state of your current filters in NSUserDefaults, these should be stored as [NSNumber numberWithBool:] } - (void)viewWillDisappear:(BOOL)animated { // store all of your filters into your NSUserDefaults as [NSNumber numberWithBool:] } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // get your cell from the table BOOL isChecked = [[_filterStates objectAtIndex:indexPath.row] boolValue]; // set the checkmark based on the current state for this filter }