重载视图后如何保存tableView单元格的复选标记使用NSUserDefaults? [英] How to save tableView cell's checkmark after reload view use NSUserDefaults?

查看:114
本文介绍了重载视图后如何保存tableView单元格的复选标记使用NSUserDefaults?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to ios development, and I'm using checkmark for cells in a UITableView.

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

I want storing checked cell in a NSUserDefaults database, When I reload the app, the checked cell that previously selected will be display, I'm try to defferent ways, but still failed to implement it.

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

Anybody can help me? I would be highly appreciate it. Sorry, my english is not good.

我的代码如下:

#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];


    }

}


推荐答案

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

It can be easy to store and retrieve checked cell(s) information with NSUserDefaults.

NSUserDefaults 可以存储Key / Value对的数据内容。在你的情况下,值可以是布尔值,键可以是 NSString &的组合。 indexpath.row 来自 UITableView

NSUserDefaults can store the data interms of Key / Value pair. Here in your case value could be boolean and key could combination of NSString & indexpath.row from UITableView.

你可以制作一个函数,如

You can make a functions like,

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

P.S。请注意,如果您的 NSArray 中的数据顺序始终保持相同的顺序,则此方法仅有用且建议。其他 NSUserDefaults 你有像 SQLite数据库 plist文件或任何其他选择!谢谢:))

P.S. Please, note that, this method will only useful and suggested if order of data from your NSArray will always in same order. Other then NSUserDefaults you've options like SQLite Database or plist file or any other option! Thanks :)

这篇关于重载视图后如何保存tableView单元格的复选标记使用NSUserDefaults?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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