ios - 如何在使用 NSUserDefaults 重新加载 View 后保存 tableView 单元格的复选标记?

标签 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];


    }

}

最佳答案

使用 NSUserDefaults 可以很容易地存储和检索选中的单元格信息。

NSUserDefaults 可以存储键/值对的数据。在您的案例中,值可以是 bool 值,键可以是 UITableView 中的 NSStringindexpath.row 的组合。

你可以做一个函数,比如,

- (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 的数据顺序始终相同时,此方法才会有用和建议。除了 NSUserDefaults,您还可以选择 SQLite Databaseplist file 或任何其他选项!谢谢:)

关于ios - 如何在使用 NSUserDefaults 重新加载 View 后保存 tableView 单元格的复选标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12120696/

相关文章:

iphone - 如何从 ZXing 获取原始字节 - iPhone

ios - 当用户在横向模式下翻转 iDevice 时,如何保持 rollY 一致?

iphone - 当 iPhone 从 sleep 状态重新打开且应用程序仍处于打开状态时释放

iOS:具有多个动态部分 objective-c 的 uitableview

ios - 表格 View 中重复复选标记

Android 圆形复选标记动画

ios - Safari/Chrome/Firefox for iOS的正文和固定div滚动问题

ios - 使用 Xamarin 在 iOS 的 UITableView 中未调用 RowSelected

swift - 快速删除每个表格单元格中的边框

ios - iOS-第一次加载表时如何在UITableView中设置选中标记