我希望在我的表视图重新加载时重新加载复选标记当我点击表格视图时添加了复选标记,当我点击第二个单元格时,旧的选中被删除了,工作正常我的问题是当我的tableview重新加载旧的复选标记应出现在单元格上。 谢谢
#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
答案 0 :(得分:2)
请写下面的步骤
在TableViewDidSelectRowAtIndexPath
中 [[NSUserDefaults standardUserDefaults] setInteger:indexPath.row forKey:@"Checked"];
[[NSUserDefaults standardUserDefaults] synchronize];
现在在CellForRowAtIndexPath
if(indexPath.row == [[NSUserDefaults standardUserDefaults]integerForKey:@"Checked"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
只检查您选择的最后一个值
答案 1 :(得分:1)
您需要存储上次检查的行索引,以便下次启动视图控制器时可以加载该值。由于您似乎只有一个部分,您只需要保存(和恢复)行索引。 NSUserDefaults
是存储此NSInteger值的好地方。您可以在每次进行新选择时保存行索引。您可以在视图控制器的viewDidLoad
方法中加载当前值。
答案 2 :(得分:1)
您可以使用另一个存储数据的类,并重新加载您使用该类的时间并正确有效地恢复数据。