gpt4 book ai didi

ios - 如何在搜索栏中搜索内容而不按iOS中的按钮?

转载 作者:行者123 更新时间:2023-12-01 18:56:34 25 4
gpt4 key购买 nike

我想在我的搜索栏中搜索内容而不按搜索按钮意味着当我在搜索栏中输入任何文本时自动搜索在我清除或取消搜索时过滤内容然后在此处显示我的主要内容列表我为搜索栏实现代码

-(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

最佳答案

您可以实现委托(delegate)方法

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;

每次用户输入字符时都会调用它。然后,您只需使用给定的字符串进行搜索。

例如,您可以调用在单击搜索本身时调用的方法:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self searchBarSearchButtonClicked:searchBar];
}

关于ios - 如何在搜索栏中搜索内容而不按iOS中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26377958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com