objective-c – 在编辑模式下将UISearchBar与UITableView一起使
发布时间:2020-12-14 17:38:33 所属栏目:百科 来源:网络整理
导读:我有一个表视图控制器,实现UISearchBarDelegate和UISearchDisplayDelegate.我有一个按钮来切换我的表格视图的编辑视图.在编辑模式下,用户可以选择多行并复制它们,删除它们等等.如果我尝试在搜索栏中输入内容,而表视图处于编辑模式,则会触发shouldReloadTable
我有一个表视图控制器,实现UISearchBarDelegate和UISearchDisplayDelegate.我有一个按钮来切换我的表格视图的编辑视图.在编辑模式下,用户可以选择多行并复制它们,删除它们等等.如果我尝试在搜索栏中输入内容,而表视图处于编辑模式,则会触发shouldReloadTableForSearchString方法,并且使用更新的谓词从核心数据中获取数据,并从函数返回YES,指示应重新加载表视图.当显示过滤结果时,它们不处于编辑模式,即使在cellForRowAtIndexPath中我可以看到tableView isEditing为YES.我无法看到多个选择的圆圈.在选择多个项目之前,我需要能够让用户过滤其列表以显着限制结果.
为什么结果没有出现在编辑模式中? 编辑模式,不使用搜索栏: 在编辑模式下搜索会生成不处于编辑模式且未设置样式的结果: 提前致谢, 这是shouldReloadTableForSearchString方法: - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { NSPredicate *predicate = nil; if ([searchString length]) { NSArray* filteredTags = nil; if ([searchString length] < 3) { // Check for tags starting with 1 or 2 characters. filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te,NSDictionary *bindings) { if ([[[te.name lowercaseString] substringToIndex:[searchString length]] isEqualToString:[searchString lowercaseString]]) return YES; return NO; }]]; } else { // Check for tags with 3 or more consecutive characters anywhere in name filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te,NSDictionary *bindings) { if ([[te.name lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound) return YES; return NO; }]]; } // OR ANY(self.tags) in %@ predicate = [NSPredicate predicateWithFormat: @"ANY(self.songlists) in %@ AND (name contains[cd] %@ OR artist_name contains[cd] %@ OR number = %@ OR ANY(tag_entities) in %@)",currentSonglist.songs,searchString,filteredTags]; [self.fetchedResultsController.fetchRequest setPredicate:predicate]; NSError *error = nil; if (![[self fetchedResultsController] performFetch:&error]) { NSLog(@"Unresolved error %@,%@",error,[error userInfo]); abort(); } // Return YES to cause the search result table view to be reloaded. } return YES; } 这是我的核心数据表视图来自.h文件的标题: @interface SonglistTVC : CoreDataTableViewController <UISearchBarDelegate,UISearchDisplayDelegate,UIPickerViewDataSource,UIPickerViewDelegate,UIActionSheetDelegate,AddSongTVCDelegate,ListSelectorDelegate> 我知道它正在使用我的表视图控制器,因为cellForRowAtIndexPath和didSelectRowAtIndexPath都在触发.我可以调试内部,并在表视图上将isEditing设置为YES.但是表格视图并没有反映它正确的编辑模式样式. 这是在搜索框中键入字母后立即触发的cellForRowAtIndexPath. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = @"SongCell"; UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier]; Song* song = [self.fetchedResultsController objectAtIndexPath:indexPath]; UILabel* artistLabel = (UILabel*)[cell viewWithTag:ARTIST_NAME_TAG]; UILabel* songLabel = (UILabel*)[cell viewWithTag:SONG_NAME_TAG]; UILabel* keyLabel = (UILabel*)[cell viewWithTag:KEY_TAG]; UILabel* numberLabel = (UILabel*)[cell viewWithTag:NUMBER_TAG]; //artistLabel.font = [UIFont systemFontOfSize:17]; [artistLabel setText: song.artist_name]; [songLabel setText: song.name]; //[songLabel setFont:[UIFont systemFontOfSize:16]]; [keyLabel setText: song.key]; [numberLabel setText: [NSString stringWithFormat:@"%@",song.number]]; UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0,320,61)]; UIView *selectedBackView = [[UIView alloc] initWithFrame:CGRectMake(0,61)]; CAGradientLayer *backGradient = [[CAGradientLayer alloc] init]; backGradient.frame = cell.layer.bounds; CAGradientLayer *selectedGradient = [[CAGradientLayer alloc] init]; selectedGradient.frame = cell.layer.bounds; if (song.status == [NSNumber numberWithUnsignedInt: 1]) { [backGradient setColors:[NSArray arrayWithObjects: (id)[[UIColor colorWithRed:240.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],nil]]; } else if (song.status == [NSNumber numberWithUnsignedInt: 2]) { [backGradient setColors:[NSArray arrayWithObjects: (id)[[UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],nil]]; } else if (song.status == [NSNumber numberWithUnsignedInt: 3]) { [backGradient setColors:[NSArray arrayWithObjects: (id)[[UIColor colorWithRed:200.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],nil]]; } [selectedGradient setColors:[NSArray arrayWithObjects: (id)[[UIColor colorWithRed:255.0f/255.0f green:160.0f/255.0f blue:60.0f/255.0f alpha:1.0f] CGColor],(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:0.7f] CGColor],nil]]; if (![self.tableView isEditing] ) { cell.backgroundView = backView; [cell.backgroundView.layer insertSublayer:backGradient atIndex:1]; cell.selectedBackgroundView = selectedBackView; [cell.selectedBackgroundView.layer insertSublayer:selectedGradient atIndex:1]; } else { //cell.backgroundView = backView; //[cell.backgroundView.layer insertSublayer:backGradient atIndex:1]; cell.selectedBackgroundView = backView; [cell.selectedBackgroundView.layer insertSublayer:backGradient atIndex:1]; } for (NSIndexPath* ip in selectedIndexes) { [self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone]; } [sortOrderPicker reloadAllComponents]; [self sendPickerDownAnimated:NO]; return cell; } 我使用分段控件内的按钮来切换编辑ON / OFF: - (IBAction)segmentChanged:(id)sender{ UISegmentedControl* sg = (UISegmentedControl*)sender; if (sg.selectedSegmentIndex == 0) { // Edit if (![self.tableView isEditing]) { [self.tableView setEditing:YES animated:YES]; [self.navigationController setToolbarHidden:NO animated:YES]; } else { [self.tableView setEditing:NO animated:YES]; [self.navigationController setToolbarHidden:YES animated:YES]; selectedIndexes = nil; } [self.tableView reloadData]; } else { // Sort if (pickerShown) [self sendPickerDownAnimated:YES]; else [self bringPickerUpAnimated:YES]; } } 解决方法
问题在于UISearchDisplayController会自动为您做很多事情.它创建自己的表视图以显示搜索结果,并管理该表视图.您可以将自己设置为该表视图的数据源和委托,并且默认情况下会自动为您完成,但它不是您的表视图而您无法控制它.如果你想要一个更大的手指定这是什么表视图,你需要设置搜索显示控制器的searchResultsTableView;或者你可以随着事情的进行获得它的引用(例如通过UISearchDisplayController自己的委托方法).但即便如此,您也不会成为管理结果表视图的UITableViewController;这完全是内部的UISearchDisplayController.如果您不喜欢这样,一个简单的解决方案是:不要使用UISearchDisplayController – 想想另一个接口.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |