ios - 如何维护 NSUserDefaults 中的单元格选择?
<p><p> <img src="/image/UaxrE.png" alt="enter image description here"/> </p>
<p> <a href="https://github.com/lminhtm/LMDropdownView" rel="noreferrer noopener nofollow">https://github.com/lminhtm/LMDropdownView</a> </p>
<p>如何在 NSUserDefault 中保存最后选择的单元格?重新打开应用程序时,应保留选择。</p>
<p>目前,只要应用打开,就会选择默认单元格。</p>
<pre><code>- (void)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
{
;
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)showDropDownViewFromDirection:(LMDropdownViewDirection)direction
{
// Init dropdown view
if (!self.dropdownView) {
self.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 () {
;
}
else {
switch (direction) {
case LMDropdownViewDirectionTop: {
self.dropdownView.contentBackgroundColor = ;
[self.dropdownView showFromNavigationController:self.navigationController
withContentView:_menuTableView];
break;
}
case LMDropdownViewDirectionBottom: {
self.dropdownView.contentBackgroundColor = ;
CGPoint origin = CGPointMake(0, CGRectGetHeight(self.navigationController.view.bounds) - CGRectGetHeight(self.moreBottomView.bounds));
[self.dropdownView showInView:self.navigationController.view
withContentView:self.moreBottomView
atOrigin:origin];
break;
}
default:
break;
}
}
}
- (void)dropdownViewWillShow:(LMDropdownView *)dropdownView
{
NSLog(@"Dropdown view will show");
}
- (void)dropdownViewDidShow:(LMDropdownView *)dropdownView
{
NSLog(@"Dropdown view did show");
}
- (void)dropdownViewWillHide:(LMDropdownView *)dropdownView
{
NSLog(@"Dropdown view will hide");
}
- (void)dropdownViewDidHide:(LMDropdownView *)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)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
LMMenuCell *cell = ;
if (!cell) {
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"menuCell"];
}
cell.menuItemLabel.text = ;
cell.selectedMarkView.hidden = (indexPath.row != self.currentMapTypeIndex);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
;
self.currentMapTypeIndex = indexPath.row;
;
}
#pragma mark - MAP VIEW DELEGATE
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotationView = [ initWithAnnotation:annotation reuseIdentifier:@"current"];
pinAnnotationView.animatesDrop = YES;
pinAnnotationView.canShowCallout = YES;
pinAnnotationView.pinColor = MKPinAnnotationColorGreen;
return pinAnnotationView;
}
#pragma mark - EVENTS
- (IBAction)titleButtonTapped:(id)sender
{
;
;
}
- (IBAction)moreButtonTapped:(id)sender
{
;
}
- (IBAction)removeAllPinsButtonTapped:(id)sender
{
;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.dropdownView.animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
;
});
}
- (IBAction)dropPinButtonTapped:(id)sender
{
;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.dropdownView.animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
MKPointAnnotation *point = [ init];
point.coordinate = ;
point.title = @"LMDropdownView";
;
});
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您在 <code>didSelectRowAtIndexPath</code> 中获得选定的索引 .. 将其存储在 userdefaults 中</p>
<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
;
self.currentMapTypeIndex = indexPath.row;
[ setInteger:self.currentMapTypeIndex forKey:@"selected_map_type"];
[synchronize];
;
}
</code></pre>
<p>在 viewDidLoad 中检索它..</p>
<pre><code>- (void)viewDidLoad
{
;
self.mapTypes = @[@"Standard", @"Satellite", @"Hybrid"];
self.currentMapTypeIndex= [[ valueForKey:@"selected_map_type"]integerValue] ? [[ valueForKey:@"selected_map_type"]integerValue] : 0;
// ... continue with your code ...
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何维护 NSUserDefaults 中的单元格选择?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36215165/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36215165/
</a>
</p>
页:
[1]