通过加载 NSUserDefaults 勾选多行

Checkmark multiple rows by loading NSUserDefaults

我要求用户在 UITableView 中 select 选项,然后将 selection 保存在 NSUserDefaults 中。我确保如果用户 select 两次选择一个选项,它只会被添加一次..即没有重复。

我正在加载 NSUserDefaults,然后尝试将这些行显示为 "checked"..以便用户记住 he/she 在 select 编辑的内容之前有 运行 个应用程序。

现在,我知道只有cellForRowAtIndexPath方法可以设置复选标记。 下面的代码只选中最后一个对象。但是如何获得多个复选标记?

这就是我的进展。

- (void)viewDidLoad {
[super viewDidLoad];
appSettings = [NSUserDefaults standardUserDefaults];
self.title = @"Select News Categories";
_selectedCategoriesArray = [[NSMutableArray alloc] init];
[self initNewsCategories];
[self loadNewsSelectedCategories];
}
#pragma mark UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [_newsCategoriesArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellNewsCategory" forIndexPath:indexPath];

// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:@"%@",[_newsCategoriesArray objectAtIndex:indexPath.row]];
NSString * cellText = [[cell textLabel] text];
for (NSString * lbl in _selectedNewsCategories)
{
    if ([cellText isEqualToString:lbl])
    {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    else
    {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

}
return cell;
}
#pragma mark UITableViewDelegate methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell * selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString * selectedCategory = [[selectedCell textLabel] text];
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryNone)
{
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
    NSLog(@"%@", selectedCategory);
    [_selectedCategoriesArray addObject:selectedCategory];
    NSLog(@"_selectedCategoriesArray:%@", _selectedCategoriesArray);
    [appSettings setObject:_selectedCategoriesArray forKey:@"selectedNewsCategories"];

}
else if ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
{
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
    NSLog(@"%@", selectedCategory);
    [_selectedCategoriesArray removeObject:selectedCategory];
    NSLog(@"_selectedCategoriesArray:%@", _selectedCategoriesArray);
    [appSettings setObject:_selectedCategoriesArray forKey:@"selectedNewsCategories"];
}
else
{
    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
[appSettings synchronize];
}
#pragma mark Helper methods
-(void) initNewsCategories
{
_newsCategoriesArray = [NSArray arrayWithObjects:@"Arts",@"Business",@"Company",
                        @"Entertainment",@"Environment",@"Health",@"Lifestyle",
                        @"Media",@"Money",@"Most Read",@"Trending",@"World",nil];
}
-(void) loadNewsSelectedCategories
{
_selectedNewsCategories = [[NSMutableArray alloc] init];
_selectedNewsCategories = [appSettings objectForKey:@"selectedNewsCategories"];

}

您的这段代码有问题:

for (NSString * lbl in _selectedNewsCategories)
{
    if ([cellText isEqualToString:lbl])
    {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    else
    {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

}

您先选中标记单元格,然后继续搜索并取消标记单元格。 你需要使用这个:

for (NSString * lbl in _selectedNewsCategories)
{
    if ([cellText isEqualToString:lbl])
    {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        break;
    }
    else
    {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

}

或者这个

if([_selectedNewsCategories indexOfObject: cellText] != NSNotFound)
{
  [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else
{
  [cell setAccessoryType:UITableViewCellAccessoryNone];
}