• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何维护 NSUserDefaults 中的单元格选择?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 15:30:12 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

enter image description here

https://github.com/lminhtm/LMDropdownView

如何在 NSUserDefault 中保存最后选择的单元格?重新打开应用程序时,应保留选择。

目前,只要应用打开,就会选择默认单元格。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapTypes = @[@"Standard", @"Satellite", @"Hybrid"];
    self.currentMapTypeIndex = 0;

    self.dropPinButton.layer.cornerRadius = 5;
    self.removeAllPinsButton.layer.cornerRadius = 5;
    self.moreButton.layer.cornerRadius = 5;
    self.moreButton.layer.shadowOffset = CGSizeZero;
    self.moreButton.layer.shadowOpacity = 0.5;
    self.moreButton.layer.shadowRadius = 1.0;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    self.menuTableView.frame = CGRectMake(CGRectGetMinX(self.menuTableView.frame),
                                          CGRectGetMinY(self.menuTableView.frame),
                                          CGRectGetWidth(self.view.bounds),
                                          MIN(CGRectGetHeight(self.view.bounds) - 50, self.mapTypes.count * 50));
    self.moreBottomView.frame = CGRectMake(CGRectGetMinX(self.moreBottomView.frame),
                                           CGRectGetMinY(self.moreBottomView.frame),
                                           CGRectGetWidth(self.view.bounds),
                                           CGRectGetHeight(self.moreBottomView.bounds));
}


#pragma mark - DROPDOWN VIEW

- (void)showDropDownViewFromDirectionLMDropdownViewDirection)direction
{
    // Init dropdown view
    if (!self.dropdownView) {
        self.dropdownView = [LMDropdownView dropdownView];
        self.dropdownView.delegate = self;

        // Customize Dropdown style
        self.dropdownView.closedScale = 0.85;
        self.dropdownView.blurRadius = 5;
        self.dropdownView.blackMaskAlpha = 0.5;
        self.dropdownView.animationDuration = 0.5;
        self.dropdownView.animationBounceHeight = 20;
    }
    self.dropdownView.direction = direction;

    // Show/hide dropdown view
    if ([self.dropdownView isOpen]) {
        [self.dropdownView hide];
    }
    else {
        switch (direction) {
            case LMDropdownViewDirectionTop: {
                self.dropdownView.contentBackgroundColor = [UIColor colorWithRed:40.0/255 green:196.0/255 blue:80.0/255 alpha:1];

                [self.dropdownView showFromNavigationController:self.navigationController
                                                withContentView:_menuTableView];
                break;
            }
            case LMDropdownViewDirectionBottom: {
                self.dropdownView.contentBackgroundColor = [UIColor whiteColor];

                CGPoint origin = CGPointMake(0, CGRectGetHeight(self.navigationController.view.bounds) - CGRectGetHeight(self.moreBottomView.bounds));
                [self.dropdownView showInView:self.navigationController.view
                              withContentView:self.moreBottomView
                                     atOriginrigin];
                break;
            }
            default:
                break;
        }
    }
}

- (void)dropdownViewWillShowLMDropdownView *)dropdownView
{
    NSLog(@"Dropdown view will show");
}

- (void)dropdownViewDidShowLMDropdownView *)dropdownView
{
    NSLog(@"Dropdown view did show");
}

- (void)dropdownViewWillHideLMDropdownView *)dropdownView
{
    NSLog(@"Dropdown view will hide");
}

- (void)dropdownViewDidHideLMDropdownView *)dropdownView
{
    NSLog(@"Dropdown view did hide");

    switch (self.currentMapTypeIndex)
    {
        case 0:
            self.mapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            self.mapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            self.mapView.mapType = MKMapTypeHybrid;
            break;
        default:
            break;
    }
}


#pragma mark - MENU TABLE VIEW

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    return [self.mapTypes count];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    LMMenuCell *cell = [tableView dequeueReusableCellWithIdentifier"menuCell"];
    if (!cell) {
        cell = [[LMMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier"menuCell"];
    }

    cell.menuItemLabel.text = [self.mapTypes objectAtIndex:indexPath.row];
    cell.selectedMarkView.hidden = (indexPath.row != self.currentMapTypeIndex);

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    self.currentMapTypeIndex = indexPath.row;

    [self.dropdownView hide];
}


#pragma mark - MAP VIEW DELEGATE

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier"current"];
    pinAnnotationView.animatesDrop = YES;
    pinAnnotationView.canShowCallout = YES;
    pinAnnotationView.pinColor = MKPinAnnotationColorGreen;
    return pinAnnotationView;
}


#pragma mark - EVENTS

- (IBAction)titleButtonTapped:(id)sender
{
    [self.menuTableView reloadData];

    [self showDropDownViewFromDirectionMDropdownViewDirectionTop];
}

- (IBAction)moreButtonTapped:(id)sender
{
    [self showDropDownViewFromDirectionMDropdownViewDirectionBottom];
}

- (IBAction)removeAllPinsButtonTapped:(id)sender
{
    [self.dropdownView hide];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.dropdownView.animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.mapView removeAnnotations:self.mapView.annotations];
    });
}

- (IBAction)dropPinButtonTapped:(id)sender
{
    [self.dropdownView hide];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.dropdownView.animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = [self.mapView convertPoint:self.mapView.center toCoordinateFromView:self.mapView];
        point.title = @"LMDropdownView";
        [self.mapView addAnnotation:point];
    });
}



Best Answer-推荐答案


您在 didSelectRowAtIndexPath 中获得选定的索引 .. 将其存储在 userdefaults 中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView deselectRowAtIndexPath:indexPath animated:NO];

     self.currentMapTypeIndex = indexPath.row;
     [[NSUserDefaults standardUserDefaults] setInteger:self.currentMapTypeIndex forKey"selected_map_type"];
     [[NSUserDefaults standardUserDefaults]synchronize];

     [self.dropdownView hide];
 }

在 viewDidLoad 中检索它..

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapTypes = @[@"Standard", @"Satellite", @"Hybrid"];
    self.currentMapTypeIndex= [[[NSUserDefaults standardUserDefaults] valueForKey"selected_map_type"]integerValue] ? [[[NSUserDefaults standardUserDefaults] valueForKey"selected_map_type"]integerValue] : 0;
   // ... continue with your code ...
}

关于ios - 如何维护 NSUserDefaults 中的单元格选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215165/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap