vendredi 21 août 2015

Retain selected checkbox in uitableview from searchbar filter

I have used UITableViewCellEditingStyle checkbox in my tableview for multiple selection. I use the uisearchbar to filter and then mark the cells..but when searchbar is closed and back to the main unfiltered table.. the previous selections disappear.

- (void)viewDidLoad {
    [super viewDidLoad];
    [tableview setEditing:YES animated:YES];
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(isfiltered==YES){
        return [filteredarr count];
    }
    else{
        if(arr==nil)return 0;

    return [arr count] ;}
}

-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID=@"cellID";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if(isfiltered==YES){
        cell.textLabel.text=[filteredarr objectAtIndex:indexPath.row];
    }
    else {
        cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    }

    cell.tintColor=[UIColor grayColor];

    return cell;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if(searchText.length==0){
        isfiltered=NO;
    }
    else{
        isfiltered=YES;
        filteredarr=[NSMutableArray array];

        for(NSString *str in arr){
            NSRange textrange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if(textrange.location != NSNotFound){
                [filteredarr addObject:str];
            }
        }
    }

    [tableview reloadData];
}

Is there anyway to keep the selections?




Aucun commentaire:

Enregistrer un commentaire