【问题标题】:How to retrieve checkbox button position change when i am searching in search bar in tableview当我在表格视图的搜索栏中搜索时如何检索复选框按钮位置更改
【发布时间】:2018-10-16 12:01:22
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    在表格视图中,您根据搜索重新调整了两个数组。取而代之的是两个数组,一个是主数组,第二个是 tempArray,并且仅在表视图中返回 tempArray。首先将所有主数组值添加到 tempArray。接下来,如果有任何搜索,则更改 tempArray。把它 tempArray 作为 NSMutableArray。

    接下来不要保留 indexPath 行以供选择。因为如果数组改变它可能会改变。所以保持objectId的选择。如果您的对象没有任何 ID。然后创建您的自定义对象并将您的原始对象更改为自定义对象。在自定义对象中设置 id 值。

    【讨论】:

    • 你能分享你的代码吗?先生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多