我在tableview中使用了UITableViewCellEditingStyle复选框进行多项选择。我使用uisearchbar过滤然后标记单元格..但是当搜索栏关闭并返回主未过滤表时...之前的选择消失。
- (void)viewDidLoad {
[super viewDidLoad];
[tableview setEditing:YES animated:YES];
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(isfiltered==YES){
return [filteredarr count];
}
else{
if(arr==nil)return 0;
return [arr count] ;}
}
-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID=@"cellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
if(isfiltered==YES){
cell.textLabel.text=[filteredarr objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text=[arr objectAtIndex:indexPath.row];
}
cell.tintColor=[UIColor grayColor];
return cell;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if(searchText.length==0){
isfiltered=NO;
}
else{
isfiltered=YES;
filteredarr=[NSMutableArray array];
for(NSString *str in arr){
NSRange textrange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(textrange.location != NSNotFound){
[filteredarr addObject:str];
}
}
}
[tableview reloadData];
}
有没有保留选择?