ios - 带有附件类型的 UITableView - 记住打勾

标签 ios objective-c uitableview

全部,

我有一个表格 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/

相关文章:

ios - Icecast 直播音频到 iPhone

ios - 在 UIImage 上添加文本

ios - 如何隐藏下一屏的过渡searchBar

android - 如何在Android中处理RecyclerView.Adapter的SELECTION?

ios - 如何使用 Swift 在 iOS 中同时调用 20 个 Web 服务?

ios - 快速检查表格 View 的滚动方式

ios - 在 UITableView 中显示一个表中的中型数据集

objective-c - 如何确定视频编解码器

objective-c - 所有 NSTableView 标题都显示排序顺序箭头?

ios - 向 Nib 启动的 View 添加边框和阴影