iphone - 如何在重新加载 View 后保存表复选标记

标签 iphone ios xcode ipad cocoa-touch

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

#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

最佳答案

请按照以下步骤编写

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

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

关于iphone - 如何在重新加载 View 后保存表复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13559937/

相关文章:

ios - 在 Map Pin Xcode 中调整 leftCalloutAccessoryView 的 UIImageView 大小

iphone - 创建协议(protocol)后访问 UIPageViewController 的委托(delegate)方法

ios - 在 iOS 中渲染水彩画

iphone - 如何检查 iPhone 上的自定义 url 方案?

ios - UITextField 不会成为 FirstResponder

ios - Xcode UI 不显示我创建的核心数据实体

objective-c - 如何遍历 objective-c 中的 NSString?

iphone - 没有从Twitter获取完整的关注者列表

iphone - 何时何地放置@class 声明

iphone - 自定义 UICollectionViewCell 出错