【问题标题】:Checkmark multiple rows by loading NSUserDefaults通过加载 NSUserDefaults 检查多行
【发布时间】:2023-03-23 05:30:01
【问题描述】:

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

我正在加载NSUserDefaults,然后尝试将行显示为“已选中”..以便用户记住他/她在之前运行应用程序时选择的内容。

现在,我知道只有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"];

}

【问题讨论】:

  • 你的表格是否允许多选

标签: ios uitableview nsuserdefaults checkmark


【解决方案1】:

这段代码有问题:

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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多