Reputation: 110
i want to restore the check mark when my table view is reloaded when i click on the table view the check mark is added and when i clicked on the 2nd cell the old checked is removed that work fine now my question is that when my tableview is reloaded the old check mark should appear on the cell. thanks
#import "mapMenuVC.h"
#import "ViewController.h"
#import "AppDelegate.h"
@interface mapMenuVC ()
@end
@implementation mapMenuVC
@synthesize checkedIndexPath;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
dataArray = [[NSMutableArray alloc]initWithObjects:@"Street Map",@"Satellite View",@"Hybird", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if([checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0];
cell.textLabel.text = [dataArray objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
switch (indexPath.row) {
case 0: {
[APPDELEGATE.viewController.myMapView setMapType:MKMapTypeStandard];
[APPDELEGATE.viewController.popoverController dismissPopoverAnimated:YES];
break;
}
case 1:
{
[APPDELEGATE.viewController.myMapView setMapType:MKMapTypeSatellite];
[APPDELEGATE.viewController.popoverController dismissPopoverAnimated:YES];
break;
}
case 2:
{
[APPDELEGATE.viewController.myMapView setMapType:MKMapTypeHybrid];
[APPDELEGATE.viewController.popoverController dismissPopoverAnimated:YES];
break;
}
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Upvotes: 2
Views: 980
Reputation: 7844
please write follow bellow steps
In TableViewDidSelectRowAtIndexPath
[[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"Checked"];
[[NSUserDefaults standardUserDefaults] synchronize];
Now In CellForRowAtIndexPath
if(indexPath.row == [[NSUserDefaults standardUserDefaults]integerForKey:@"Checked"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
It only CheckMarked the last value you have selected
Upvotes: 2
Reputation: 2219
you can use another class that is store your data and reload time you used that class and restore your data correctly and efficiently.
Upvotes: 1
Reputation: 318774
You need to store the last checked row index so you can load that value the next time you bring up the view controller. Since you appear to only have one section you just need to save (and restore) the row index. NSUserDefaults
is a good place to store this NSInteger value. You can save the row index each time a new selection is made. You can load the current value in the view controller's viewDidLoad
method.
Upvotes: 1