如何在iOS中不按按钮在搜索栏中搜索内容?
我希望在我的搜索栏中搜索内容而不按搜索按钮意味着当我在搜索栏中输入任何文本时自动搜索过滤内容当我清除或取消搜索时,然后在此处显示我的主要内容列表我为搜索栏实现代码如何在iOS中不按按钮在搜索栏中搜索内容?,ios,objective-c,uisearchbar,Ios,Objective C,Uisearchbar,我希望在我的搜索栏中搜索内容而不按搜索按钮意味着当我在搜索栏中输入任何文本时自动搜索过滤内容当我清除或取消搜索时,然后在此处显示我的主要内容列表我为搜索栏实现代码 -(void)SearchBarCode { self.disableViewOverlay.backgroundColor=[UIColor blackColor]; self.disableViewOverlay.alpha = 0; theSearchBar = [[UISearchBar alloc]
-(void)SearchBarCode
{
self.disableViewOverlay.backgroundColor=[UIColor blackColor];
self.disableViewOverlay.alpha = 0;
theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(-61, 46, 378.0f, 50)];
theSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 46, 310.0, 44.0)];
searchBarView.autoresizingMask = 0;
theSearchBar.delegate =self;
[searchBarView addSubview:theSearchBar];
self.navigationItem.title=@"Insta SMS Collection";
self.navigationItem.titleView=searchBarView;
theSearchBar.placeholder = @"Search";
}
- (void)viewDidAppear:(BOOL)animated
{
[self.theSearchBar resignFirstResponder];
[super viewDidAppear:animated];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
theSearchBar.showsScopeBar = YES;
[theSearchBar invalidateIntrinsicContentSize];
[theSearchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[theSearchBar invalidateIntrinsicContentSize];
[theSearchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
scrollView.scrollEnabled = TRUE;
theSearchBar.text = @"";
theSearchBar.text = @"";
[theSearchBar setShowsCancelButton: NO animated: YES];
[theSearchBar resignFirstResponder];
}
- (void)searchTableList
{
NSString *searchString = theSearchBar.text;
filteredContentList = [[NSMutableArray alloc]init];
for (SMSCategory *cat in Arrayobject)
{
NSString *tempStr = cat.Name;
NSComparisonResult result = [tempStr compare:searchString options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0,
[searchString length])];
if (result == NSOrderedSame)
{
[filteredContentList addObject:cat];
}
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
scrollView.scrollEnabled = TRUE;
[theSearchBar resignFirstResponder];
[theSearchBar setShowsCancelButton: NO animated: YES];
[self searchTableList];
[self DynamicButton:filteredContentList];
[self viewDidAppear:true];
}
.h文件代码
@interface CategoryMainWindowViewController : UIViewController<UISearchBarDelegate,UITextViewDelegate>{
NSMutableArray *Arrayobject;
UIView *disableViewOverlay;
UITableView *theTableView;
UISearchBar *theSearchBar;
NSMutableArray *filteredContentList;
BOOL isSearching;
}
@property(retain) UIView *disableViewOverlay;
@property(retain) NSMutableArray *Arrayobject;
@property (nonatomic, retain) IBOutlet UITableView *theTableView;
@property (nonatomic, retain) IBOutlet UISearchBar *theSearchBar;
@property(nonatomic,retain) UIScrollView *scrollView;
@property(strong,nonatomic) NSString *databasePath;
-(void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active;
-(void)DynamicButton:(NSMutableArray*)objectName;
-(NSMutableArray*)GetData;
@end
@接口类别MainWindowViewController:UIViewController{
NSMutableArray*阵列对象;
UIView*禁用视图覆盖;
UITableView*表格视图;
ui搜索栏*搜索栏;
NSMutableArray*filteredContentList;
布尔正在研究;
}
@属性(保留)UIView*disableViewOverlay;
@属性(保留)NSMutableArray*Arrayobject;
@属性(非原子,保留)IBUITableView*TableView;
@属性(非原子,保留)IBUI搜索栏*搜索栏;
@属性(非原子,保留)UIScrollView*scrollView;
@属性(强,非原子)NSString*数据库路径;
-(无效)搜索栏:(UISearchBar*)搜索栏激活:(BOOL)激活;
-(void)DynamicButton:(NSMutableArray*)objectName;
-(NSMutableArray*)获取数据;
@结束
您可以在委托方法中使用搜索栏.text,搜索Bartextdidediting或
搜索栏:textDidChange:您可以实现委托方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
每次用户输入字符时都会调用此函数。然后只需使用给定的字符串进行搜索
例如,您可以调用在单击搜索本身时调用的方法:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self searchBarSearchButtonClicked:searchBar];
}
将Arrdata分配给viewdidload方法中的ArrTemp,然后使用此方法进行搜索如果搜索字段包含空文本,则它将显示以前存储在ArrTemp中的主要内容
}
}请注意我对您的代码格式所做的更改。在向stackoverflow发布问题时,请保持这样。另外,方法以小写开头,
DynamicButton:
应该是DynamicButton:
。您也并没有列出您尝试过的任何内容,您只是发布了您现有的代码,说您想做其他事情,您尝试了什么?使用DidBeginEditing,它只会搜索一次。他想要的(当然,据我所知)是每次用户输入/更改文本时进行搜索。如何对取消按钮进行编码意味着当我的搜索完成时,我想按取消按钮转到主内容列表。当我按取消按钮转到初始状态时,我会像您在searchBarCancelButtonClicked方法中所做的一样。有人打电话吗?发生了什么?您是否在方法中设置了断点?它是否会在断点处停止?但会调用其他委托方法,如textDidBeginEditing/EndEditing?在那里也放一个断点。
- (void)viewDidLoad
{
[super viewDidLoad];
ArrTemp = [[NSMutableArray alloc]initWithArray:Arrdata];
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length >0) {
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"vName CONTAINS[cd] %@ OR vName LIKE[cd] %@",
searchText,searchText];//
NSArray *filteredArray = [Arrdata filteredArrayUsingPredicate:predicate];
Arrdata = [[NSMutableArray alloc]initWithArray:filteredArray];
}else{
Arrdata = ArrTemp;
}
[tblview reloadData];