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

时间:2014-07-26 02:09:52

标签: ios objective-c uitableview nsuserdefaults

所以我实现了一个带有tableview的UIViewController,基本上它是作为一组"过滤器"加载的。对于我的uicollectionview。

现在,当我点击我的tableview中的复选标记时,它会"过滤"相应的我的单元格,但现在当我再次重新加载视图时,我想显示最近的"复选标记"我已经使用过,或者"过滤器。"

我已经看到这是用NSUserDefaults实现的,但是我无法成功实现它。

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

CODE

FiltersViewController.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

7 个答案:

答案 0 :(得分:4)

您还需要检查cellForRowAtIndexPath。在此

中写下此代码
if([[NSUserDefaults standardUserDefaults] objectForKey:[self getKeyForIndex:indexPath.row]])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

是的,不要忘记在didSelectRowAtIndexPath中调用此方法

[self checkedCellAtIndex:indexPath.row];

享受。

答案 1 :(得分:3)

你做的几乎一切都正确。您只需要在cellForRowAtIndexPath委托方法中使用逻辑来读取NSUserDefault值(对于选中的复选框)。这是在屏幕上显示UITableViewCells时绘制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;
}

答案 2 :(得分:2)

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


}

答案 3 :(得分:1)

你已经完美实施了。您只需修改cellForRowAtIndexPath方法

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

还可以从UITableView的[self checkedCellAtIndex:indexPath.row];方法调用didSelectRowAtIndexPath

答案 4 :(得分:1)

使用NSUserDefaults保存tableView CheckMark的最佳方法

    @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

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

didSelectRowAtIndexPath方法

 -(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 CheckMarks的最佳方式,它现在简单高效,现在只有一个数组用于保存和加载,代码减少了太多:)

答案 5 :(得分:0)

您应该在调用NSUserDefaults

时在cellForRowAtIndexPath中加载数据
- (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;
}

答案 6 :(得分:0)

我认为您要查找的是在进入和退出视图时获取过滤器表的当前状态。您需要利用- (void)viewWillDisappear:(BOOL)animated将表的当前状态保存到NSUserDefaults,然后使用- (void)viewDidLoad- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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
}