简体   繁体   中英

How to retrieve checkbox button position change when i am searching in search bar in tableview

在此处输入图片说明

for(int i=0; i<[devices count]; i++) { 
    [_cellDataArray addObject:@"Uncheck"];
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if(searchText.length==0) {
        isfilert=false; 
    }
    else { 
        isfilert=true;
        filterarray=[[NSMutableArray alloc]init];
        for ( NSString * str in devices) {
            NSRange rangename = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (rangename.location == !NSNotFound) { 
                //  filterarray=[[NSMutableArray alloc]init];
                [filterarray addObject:str]; 
            }
        }
    }
    [self.tableView reloadData];  
}

 - (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
 }

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (isfilert) {
        return [filterarray count]; 
    } 
    else {
        return [devices count]; 
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    UILabel *lab1 = (UILabel *)[cell viewWithTag:100];

    UIButton *button = (UIButton *)[cell viewWithTag:90];
    if (isfilert) {
        lab1.text = filterarray[indexPath.row];
    } 
    else {
        lab1.text = devices[indexPath.row];
    }

    if([[_cellDataArray objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"]) {
        [button setImage:[UIImage imageNamed:@"checked-symbol1.png"] forState:UIControlStateNormal];
    } 
    else {
        [button setImage:[UIImage imageNamed:@"redcircle.png"] forState:UIControlStateNormal];
    }
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)buttonClicked:(id)sender {
    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];

    UIButton *button = (UIButton *)sender;

    if([[_cellDataArray objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"]) {
        [button setImage:[UIImage imageNamed:@"redcircle.png"] forState:UIControlStateNormal];
        [_cellDataArray replaceObjectAtIndex:indexPath.row withObject:@"Check"];
        [_array2 addObject:[devices objectAtIndex:indexPath.row]];
    }
    else {
        [button setImage:[UIImage imageNamed:@"checked-symbol1.png"] forState:UIControlStateNormal];
        [_cellDataArray replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];

        [_array2 removeObject:[devices objectAtIndex:indexPath.row]];
    }
}

在此处输入图片说明

In table view you retuned two arrays based on searching. Instead of that take two arrays one for main and second one is tempArray and returned only tempArray in table View. First add all main array values into tempArray. Next if any Search is there then change the tempArray. Take it tempArray as NSMutableArray.

Next don't hold the indexPath rows for selection. Because it may change if array changed. So hold objectId's in selection. If you don't have any IDs to your objects. Then create your custom object and change your original objects to custom objects. In custom Object set id value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM