Как получить изменение положения кнопки-флажка при поиске в строке поиска в табличном представлении - PullRequest
0 голосов
/ 16 октября 2018

enter image description here

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

enter image description here

1 Ответ

0 голосов
/ 16 октября 2018

В табличном представлении вы перенастроили два массива на основе поиска.Вместо этого возьмите два массива, один для основного, а второй для tempArray и вернул только tempArray в табличном представлении.Сначала добавьте все основные значения массива в tempArray.Затем, если есть какой-либо поиск, измените tempArray.Возьмите в качестве значения tempArray NSMutableArray.

Далее не храните строки indexPath для выбора.Потому что это может измениться, если массив изменился.Так что держите objectId в выборе.Если у вас нет идентификаторов для ваших объектов.Затем создайте свой пользовательский объект и измените ваши оригинальные объекты на пользовательские объекты.В пользовательском объекте задайте значение id.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...