这是我的代码,有人帮助我吗? 在该示例中,我显示项目列表,并且在搜索期间,通过使用filterarray在表格视图中显示搜索项目。 但我需要在重新加载表视图后在搜索期间保持选择。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.tableView.allowsMultipleSelection = YES;
_button=[[UIButton alloc]init];
_tableListArray=[NSMutableArray arrayWithObjects:@"Suresh kumar",@"Nanda kumar",@"Cheese",@"Jam",@"Pizza",@"Burger",@"Roll Pizza",@"Butter Bread",@"Bread",nil];
_filteredListArray=[[NSMutableArray alloc]initWithArray:_tableListArray];
NSLog(@"fiter=>%@",_filteredListArray) ;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
// return [_tableListArray count];
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return [_tableListArray count];
return [_filteredListArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"Cell";
UITableViewCell *cell=[tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text=[_filteredListArray objectAtIndex:indexPath.row];
NSLog(@"filterarray=>%@",_filteredListArray);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// i want to know what can i do here
}
#pragma mark-TextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
_textFld.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(@"textfield%@",_textFld);
NSString *myString=_textFld.text;
if (myString.length==0) {
_filteredListArray=_tableListArray;
}
else
{
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"self contains[cd] %@",myString];
_filteredListArray=[NSMutableArray arrayWithArray:[_tableListArray filteredArrayUsingPredicate:predicate]];
}
[_tableView reloadData];
return NO;
}
@end
答案 0 :(得分:0)
保留NSMutableArray
NSIndexSet
。在每个tableView:didSelectRowAtIndexPath:
检查indexSet
是否在数组中
1.如果是,请将其取下
2.如果没有,请添加
重新加载tableView时,此数组不会更改。