全部,
我有一个表格 View ,用户勾选他们的城市,然后显示附件,然后我将该城市存储为 NSUSerDefault。如果他们需要再次更改城市,那么最好的方法是什么,最后一个仍然打勾?
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [alllocationsCity objectAtIndex:indexPath.row];
cell.accessoryType = (indexPath.row == selectedRow)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.CityTableView deselectRowAtIndexPath:indexPath animated:YES];
[self.CityTableView setTintColor:[UIColor grayColor]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.CityTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[[NSUserDefaults standardUserDefaults] setObject:alllocationsCity[indexPath.row] forKey:@"CitySelected"];
selectedRow = indexPath.row;
[tableView reloadData];
}
最佳答案
最好你可以保留一个可变数组,其中包含所有选定的城市名称并将其保存为默认值,例如
阅读评论
- (void)viewDidLoad
{
[super viewDidLoad];
_cities = [[NSMutableArray alloc]initWithObjects:@"city1",@"city2",@"city3",@"city4", nil];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; //initially get all the selected cities
selecedArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"SEL_CITIES"]]; //store them in an mutable array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [_cities objectAtIndex:indexPath.row];
cell.accessoryType = ([selecedArray containsObject:[_cities objectAtIndex:indexPath.row]]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; //set the checkmark based on weather the city name is present or not
return cell;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
//update the array based on selection
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
if([selecedArray containsObject:[_cities objectAtIndex:indexPath.row]])
{
[selecedArray removeObject:[_cities objectAtIndex:indexPath.row]];
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selecedArray addObject:[_cities objectAtIndex:indexPath.row]];
}
//update the defaults each time for selection or deselection
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:selecedArray forKey:@"SEL_CITIES"];
[defaults synchronize];
}
希望对你有帮助...
关于ios - 带有附件类型的 UITableView - 记住打勾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23358417/