[英]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]];
}
}
在表視圖中,您根據搜索重新調整了兩個數組。 取而代之的是兩個數組,一個用於主數組,第二個數組是 tempArray 並在表視圖中僅返回 tempArray。 首先將所有主數組值添加到 tempArray。 接下來,如果有任何搜索,則更改 tempArray。 將 tempArray 視為 NSMutableArray。
接下來不要保留用於選擇的 indexPath 行。 因為如果數組改變它可能會改變。 所以在選擇中保持objectId。 如果您的對象沒有任何 ID。 然后創建自定義對象並將原始對象更改為自定義對象。 在自定義對象中設置 id 值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.