Ios 当用户多次加载视图时,在uitableview上保存附件复选标记

Ios 当用户多次加载视图时,在uitableview上保存附件复选标记,ios,objective-c,uitableview,nsuserdefaults,Ios,Objective C,Uitableview,Nsuserdefaults,因此,我用tableview实现了一个UIViewController,它基本上作为一组“过滤器”加载到我的uicollectionview中 现在,当我单击tableview中的复选标记时,它会相应地“过滤”我的单元格,但现在当我再次重新加载视图时,我希望显示我使用过的最新“复选标记”或“过滤器” 我已经看到使用NSUserDefaults实现了这一点,但我无法成功实现这一点 如果有人能帮助我,我将不胜感激 代码 filterViewController.m: #import "Filters

因此,我用tableview实现了一个UIViewController,它基本上作为一组“过滤器”加载到我的uicollectionview中

现在,当我单击tableview中的复选标记时,它会相应地“过滤”我的单元格,但现在当我再次重新加载视图时,我希望显示我使用过的最新“复选标记”或“过滤器”

我已经看到使用NSUserDefaults实现了这一点,但我无法成功实现这一点

如果有人能帮助我,我将不胜感激

代码

filterViewController.m:

#import "FiltersViewController.h"

@interface FiltersViewController ()

@property (nonatomic, strong) NSMutableSet *selectedRowObjects;
//@property (nonatomic, strong) NSArray *filters;

@end

@implementation FiltersViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.selectedRowObjects = [NSMutableSet setWithCapacity:10];
}

- (IBAction)filtersSelected:(id)sender {
    [self.delegate filtersSelected:self.selectedRowObjects];
}

- (IBAction)cancelFilterSelection:(id)sender {
    [self.delegate filterSelectionCancelled];
}

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


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];



    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *obj = cell.textLabel.text;

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.selectedRowObjects removeObject:obj];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.selectedRowObjects addObject:obj];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
@end

你几乎每件事都做对了。您只需要在
cellforrowatinexpath
delegate方法中放入用于读取NSUserDefault值(对于复选框)的逻辑。这是当UITableViewCells显示在屏幕上时绘制它们的方法。大概是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];

    if ([self getCheckedForIndex:indexPath.row])
    {
        //code to set checkbox
    }

    return cell;
}

您还需要签入CellForRowatineXpath。用这种方式编写代码

if([[NSUserDefaults standardUserDefaults] objectForKey:[self getKeyForIndex:indexPath.row]])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
是的,别忘了在DidSelectRowatineXpath中调用这个方法

[self checkedCellAtIndex:indexPath.row];
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = TableTitles[indexPath.row];

    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    if ([SelectedRows containsObject:obj])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    if ([SelectedRows containsObject:obj])
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        [SelectedRows removeObject:obj];
        [tableView reloadData];
    }else{
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
        [SelectedRows addObject:obj];
        [tableView reloadData];
    }

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    [userDef setObject:SelectedRows forKey:@"SelectedRows"];
    [userDef synchronize];
}

享受。

您已经完美地实现了。您只需要修改cellforrowatinexpath方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];

BOOL checked = [self getCheckedForIndex:indexPath.row];
   if(checked)
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
   else
       cell.accessoryType = UITableViewCellAccessoryNone;


    return cell;
}

也可以调用
[self-checkedCellAtIndex:indexPath.row]从UITableView的方法中选择RowatineXpath。

使用NSUserDefaults保存tableView复选标记的最佳方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = ArrayOfObject[indexPath.row];
NSUserDefaults *ud =[NSUserDefaults standardUserDefaults];
if([[ud objectForKey:@"selectedObjectKey "]isEqualToString:ArrayOfObject[indexPath.row]])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;


}
    @interface TableViewController (){
    NSArray *TableTitles;
    NSMutableArray *SelectedRows;
}
viewDidLoad

   - (void)viewDidLoad
{
    [super viewDidLoad];

    // table view array
    TableTitles = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:@"SelectedRows"]];
}
cellForRowAtIndexPath

[self checkedCellAtIndex:indexPath.row];
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = TableTitles[indexPath.row];

    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    if ([SelectedRows containsObject:obj])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    if ([SelectedRows containsObject:obj])
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        [SelectedRows removeObject:obj];
        [tableView reloadData];
    }else{
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
        [SelectedRows addObject:obj];
        [tableView reloadData];
    }

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    [userDef setObject:SelectedRows forKey:@"SelectedRows"];
    [userDef synchronize];
}
didSelectRowAtIndexPath

[self checkedCellAtIndex:indexPath.row];
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = TableTitles[indexPath.row];

    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    if ([SelectedRows containsObject:obj])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *obj = [NSNumber numberWithInteger:indexPath.row];
    if ([SelectedRows containsObject:obj])
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        [SelectedRows removeObject:obj];
        [tableView reloadData];
    }else{
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
        [SelectedRows addObject:obj];
        [tableView reloadData];
    }

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    [userDef setObject:SelectedRows forKey:@"SelectedRows"];
    [userDef synchronize];
}
更新


我用一种新的方法更新了所有代码,这是保存TableView复选标记的最佳方法,现在它简单高效,现在只使用一个数组来保存和加载,代码减少了很多:)

在调用
cellForRowAtIndexPath
时,您应该在
NSUserDefaults
中加载数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filter" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];

    //You should set accessoryType here by NSUserDefaults data
    if ([self getCheckedForIndex:indexPath.row]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


    return cell;
}

我认为您需要的是在进入和退出视图时获取过滤器表的当前状态。您需要使用
-(void)视图将消失:(BOOL)动画
将表的当前状态保存为NSUserDefaults,然后使用
-(void)viewDidLoad
-(UITableViewCell*)表视图:(UITableView*)表视图单元格FORROWATINDEXPATH:(NSIndexPath*)indexPath
用于在表重新加载时设置复选标记

@interface FiltersViewController () {
    NSMutableArray *_filterStates;
}

@end

- (void)viewDidLoad {
    filterStates = [[NSMutableArray alloc] init];

    // get state of your current filters in NSUserDefaults, these should be stored as [NSNumber numberWithBool:]
}

- (void)viewWillDisappear:(BOOL)animated {
    // store all of your filters into your NSUserDefaults as [NSNumber numberWithBool:]
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // get your cell from the table

    BOOL isChecked = [[_filterStates objectAtIndex:indexPath.row] boolValue];

    // set the checkmark based on the current state for this filter
}

您正在获取和设置代码中其他地方未引用的方法中的用户默认值。@duci9y好的,您能用代码向我解释一下吗?我理解你的意思。记录你的用户默认值(在几个点上),看看它们是否符合你的期望。这根本不起作用。现在,当我加载表格视图时,每次打开它时,它都会显示所有的复选标记。我想我对规格不太清楚。也许你可以附上一些屏幕截图,准确地显示你想要完成的事情?这根本不起作用。现在,当我加载表视图时,它会在每次打开时显示所有复选标记。您的要求是brother ok,但在重新加载时必须删除nsuserdefault尝试此[[NSUserDefaults standardUserDefaults]removeObjectForKey:@“在此处传递密钥”];您的需求是存储在NSUserDefault中,如果您不使用NSUserDefault,那么使用ArrayPartially Working非常容易。当我取消选中时,应用程序重新启动并再次选中。