我想在我的搜索栏中搜索内容而没有按下搜索按钮意味着当我在搜索栏中输入任何文本时,当我清除或取消搜索时自动搜索过滤内容然后在此处显示我的主要内容列表我实现了搜索栏的代码
-(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
答案 0 :(得分:2)
您可以实现委托方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
每次用户输入字符时都会调用此方法。然后,您只需要使用给定的字符串进行搜索。
例如,您可以调用单击搜索本身时调用的方法:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self searchBarSearchButtonClicked:searchBar];
}
答案 1 :(得分:1)
在Viewdidload mehod中将Arrdata分配给ArrTemp,然后使用此方法搜索搜索字段是否包含空文本,然后它将显示之前存储在ArrTemp中的主要内容......
- (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];
}
答案 2 :(得分:0)
您可以在委托方法中使用 theSearchBar.text ,searchBarTextDidEndEditing或 搜索栏:textDidChange: