如何在附件checkmark-iphone的userdefaults中保存状态 [英] how to save the state in userdefaults of accessory checkmark-iphone

查看:78
本文介绍了如何在附件checkmark-iphone的userdefaults中保存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理具有UItableView的应用程序.我可以在表格的行中放置复选标记.现在如何保存复选标记的状态,以便即使用户关闭了应用程序 状态应该被刮掉,并在下次启动应用程序时显示选中标记. 我已按照NSUSerDefaults上的教程进行操作,但将代码放在保存和检索的位置上.我曾尝试过,但是每次错误都塞满了我并且无法修复. 我的代码:

I am working on an application having UItableView. In the rows of the table i am able to put checkmarks.Now how to save the state of checkmarks so that even if user close the application the state should be shaved and in the next launch of application checkmark should be shown. i have followed the tutorials on NSUSerDefaults but Pulling my hairs where to put the codes of saving and retrieving.I have tried but every time errors are stuffing me and not able to fix. My Code:

MY.h文件

**@protocol LocationSelectionViewControllerDelegate <NSObject>

@required
- (void)rowSelected:(NSString *)selectedValue selectedIndex:(NSInteger)index;
@end**

@interface LocationSelection : UIViewController <UITableViewDelegate,UITableViewDataSource>{

UITableView *table;
***NSInteger       selectedIndex;***
NSMutableArray *menuList;
***id <LocationSelectionViewControllerDelegate>    delegate;***

}
@property (nonatomic,retain) NSMutableArray *menuList;  
@property (nonatomic,retain) IBOutlet UITableView *table;  
***@property (nonatomic, assign) id <LocationSelectionViewControllerDelegate> delegate;**  
**@property NSInteger selectedIndex;***  
@end  

我的.m文件:

@implementation LocationSelection  
***@synthesize menuList, table,selectedIndex,delegate;***

- (void)viewDidLoad
{
    menuList = [[NSMutableArray alloc] initWithObjects:
                [NSArray arrayWithObjects:@"LOCATION1", nil],
                [NSArray arrayWithObjects:@"LOCATION2", nil],
                [NSArray arrayWithObjects:@"LOCATION3", nil],
                nil];

    self.title = @"Location Selection";

    [table reloadData];
    [super viewDidLoad];
}

//MY CELLFORROWATINDEXPATH  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
    }
    cell.highlighted = NO;

    ***NSArray * rowArray = [menuList objectAtIndex:indexPath.row];***

    UILabel * nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)] autorelease];
    nameLabel.text = [NSString stringWithFormat:@"%@", [rowArray objectAtIndex:0]];
    [cell.contentView addSubview:nameLabel];

    ***cell.accessoryType = (rowArray == selectedIndex && selectedIndex > -1 && selectedIndex < [menuList count]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;
    ***
}

//MY DIDSELECTROWATINDEXH  

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int newRow = [indexPath row];

    if (newRow != selectedIndex)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        if (selectedIndex > - 1 && selectedIndex < [menuList count])
        {
            NSUInteger newIndex[] = {0, selectedIndex};
            NSIndexPath *lastIndexPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
        }       

        selectedIndex = newRow;  
        NSString *selectedValue=[menuList objectAtIndex:selectedIndex];
        [self.delegate rowSelected:selectedValue selectedIndex:selectedIndex];
    }
}

推荐答案

您好,如果您有一个对象来指示选中标记,它会很适合您.您可以通过synchronize将其保存为用户默认设置.在cellForRowATIndexPath:中,您可以检查用户默认值中是否存在该值,如果是,则将单元格附件标记为选中标记,如果不存在,则将其选中.

Hi I think it will suit you if you have an object(s) to indicate the checkmark. You can save this to the user defaults through synchronize. In the cellForRowATIndexPath: you can check if the value is present in the user defaults and if yes make the cell accessory as checkmarked and if its not present make it none.

这篇关于如何在附件checkmark-iphone的userdefaults中保存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆