重载视图后如何保存tableView单元格的复选标记使用NSUserDefaults?

时间:2012-08-25 09:14:18

标签: ios uitableview checkmark

我是ios开发的新手,我正在使用UITableView中的单元格的复选标记。

我希望将已检查的单元格存储在NSUserDefaults数据库中,当我重新加载应用程序时,将显示之前选中的已检查单元格,我尝试不同的方式,但仍未能实现它。

任何人都可以帮助我吗?我将非常感激。对不起,我的英语不好。

我的代码如下:

#import "FirstRowSubview.h"

@interface FirstRowSubview ()

@end

@implementation FirstRowSubview

@synthesize array = _array;
@synthesize lastIndexPath = _lastIndexPath;


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:@"ffgfgh", @"564654", @"56548", @"fgmjfgmf", @"ggkdj", nil];
    self.array = list;
    [list release];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.array = nil;
}


#pragma mark -
#pragma mark - TableView Datasource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *subviewCells = @"Cells";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells];
    }
    NSUInteger oldRow = [_lastIndexPath row];
    cell.accessoryType = (indexPath.row == oldRow && _lastIndexPath != nil) ?
    UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    cell.textLabel.text = [_array objectAtIndex:indexPath.row];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithInt:_lastIndexPath.row] forKey:@"lastIndexPath"];
    [defaults synchronize];



    return cell;
 }

#pragma mark -
#pragma mark - TableView Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int newRow = [indexPath row];
    int oldRow = (_lastIndexPath != nil) ? [_lastIndexPath row] : -1;

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

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;

        [indexPath retain];
        [_lastIndexPath release];
        _lastIndexPath = indexPath;


        [tableView deselectRowAtIndexPath:indexPath animated:YES];


    }

}

3 个答案:

答案 0 :(得分:9)

使用NSUserDefaults可以轻松存储和检索已检查的单元格信息。

NSUserDefaults可以存储Key / Value对的数据中间件。在您的情况下,值可以是布尔值,键可以是NSString&的组合。来自indexpath.row的{​​{1}}。

你可以创建像

这样的函数
UITableView

P.S。请注意,只有来自- (NSString *)getKeyForIndex:(int)index { return [NSString stringWithFormat:@"KEY%d",index]; } - (BOOL) getCheckedForIndex:(int)index { if([[[NSUserDefaults standardUserDefaults] valueForKey:[self getKeyForIndex:index]] boolValue]==YES) { return YES; } else { return NO; } } - (void) checkedCellAtIndex:(int)index { BOOL boolChecked = [self getCheckedForIndex:index]; [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:!boolChecked] forKey:[self getKeyForIndex:index]]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_array count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //Here, you can check for previously checked cells like static NSString *subviewCells = @"Cells"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:subviewCells]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subviewCells]; } cell.textLabel.text = [_array objectAtIndex:indexPath.row]; if([self getCheckedForIndex:indexPath.row]==YES) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; //Use checkedCellAtIndex for check or uncheck cell UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [self checkedCellAtIndex:indexPath.row]; if([self getCheckedForIndex:indexPath.row]==YES) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } } 的数据顺序始终保持相同的顺序,此方法才有用且建议使用。除NSArray之外的其他选项,例如NSUserDefaultsSQLite Database或任何其他选项!谢谢:))

答案 1 :(得分:3)

这是如何根据表的当前选择创建NSIndexPaths数组,然后将该数组保存到NSUserDefaults中的基本示例。

 -(void)viewDidLoad
{
    [super viewDidLoad];
    for (NSIndexPath *indexPath in [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"mySavedMutableArray"]) {
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

您可以在此处将对象添加到数组中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"mySavedMutableArray"] addObject:indexPath];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

在这里,您将从阵列中删除取消选择的对象。

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"mySavedMutableArray"] removeObject:indexPath];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

答案 2 :(得分:0)

让代码正常工作的最简单方法就是在视图中添加一行,如下所示:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults valueForKey:@"lastIndexPath"])
_lastIndexPath = [defaults valueForKey:@"lastIndexPath"];

这将解决您的问题:)

快乐编码:)