简体   繁体   English

当我在 tableview 的搜索栏中搜索时如何检索复选框按钮位置更改

[英]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.取而代之的是两个数组,一个用于主数组,第二个数组是 tempArray 并在表视图中仅返回 tempArray。 First add all main array values into tempArray.首先将所有主数组值添加到 tempArray。 Next if any Search is there then change the tempArray.接下来,如果有任何搜索,则更改 tempArray。 Take it tempArray as NSMutableArray.将 tempArray 视为 NSMutableArray。

Next don't hold the indexPath rows for selection.接下来不要保留用于选择的 indexPath 行。 Because it may change if array changed.因为如果数组改变它可能会改变。 So hold objectId's in selection.所以在选择中保持objectId。 If you don't have any IDs to your objects.如果您的对象没有任何 ID。 Then create your custom object and change your original objects to custom objects.然后创建自定义对象并将原始对象更改为自定义对象。 In custom Object set id value.在自定义对象中设置 id 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在表格视图中更改ADD(+)和MINUS(-)按钮的位置 - How to change ADD(+) and MINUS(-) button position in tableview 在默认搜索栏中搜索字符串为空时启用搜索按钮 - Enable Search button when searching string is empty in default search bar 更改搜索栏的位置 - Change position for search bar 当显示搜索栏时(如在Apple的邮件应用程序中),如何在工具栏中放置tableView的“编辑”按钮? - How to put a tableView's Edit button in the toolbar when a search bar is shown (as in Apple's mail app)? 在tableview中搜索时如何更改文本“无结果”? - how to change the text “no results” when search in tableview? 如何使用栏按钮项添加搜索栏,我正在使用核心数据? - How to add search bar with bar button item, I am using core data? 如何更改左栏按钮项的位置? - How to change the position of the left bar button item? 单击搜索栏上的“取消”时如何重新加载tableview中的所有数据?(IOS / Swift) - How can I reload all of data in tableview when I click cancel on search bar?(IOS/Swift) 是否可以更改搜索栏中取消按钮的垂直位置? - Is it possible to change the vertical position of cancel button in the search bar? 如何根据TableView行删除来更改导航栏按钮项的状态 - How do i change the navigation bar button item state according to tableview row delete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM