2

我想在我的表格视图重新加载时恢复复选标记,当我单击表格视图时添加复选标记,当我单击第二个单元格时,旧的检查被删除,现在工作正常我的问题是,当我的表格视图是重新加载旧复选标记应出现在单元格上。谢谢

#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
4

3 回答 3

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;
}

它只勾选了您选择的最后一个值

于 2012-11-26T07:09:36.670 回答
1

您需要存储最后检查的行索引,以便下次打开视图控制器时加载该值。由于您似乎只有一个部分,您只需要保存(和恢复)行索引。NSUserDefaults是存储这个 NSInteger 值的好地方。每次进行新选择时,您都可以保存行索引。viewDidLoad您可以在视图控制器的方法中加载当前值。

于 2012-11-26T07:03:55.137 回答
1

您可以使用另一个类来存储您的数据并重新加载您使用该类的时间,并正确有效地恢复您的数据。

于 2012-11-26T07:04:44.753 回答