【发布时间】: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