Ios 文本字段作为搜索栏
有没有什么好的教程或方法可以将文本字段变成搜索栏 我已经用search bar+search display controller设置了我的搜索栏,它工作正常——但很难定制……特别是去掉难看的灰色覆盖,显然这是最好的方法Ios 文本字段作为搜索栏,ios,objective-c,search,uitextfield,uisearchbar,Ios,Objective C,Search,Uitextfield,Uisearchbar,有没有什么好的教程或方法可以将文本字段变成搜索栏 我已经用search bar+search display controller设置了我的搜索栏,它工作正常——但很难定制……特别是去掉难看的灰色覆盖,显然这是最好的方法 我找不到任何关于做这件事的信息,我想一定有人以前做过。我不知道有任何教程涉及这一主题,但不久前,在其他人的帮助下,我完成了这项工作。下面是.h和.m文件中的全部代码。我可以解释整件事,但每一行都有评论,所以我想这已经足够清楚了 h 我希望以上内容足够清晰和有用,如果您需要帮助,
我找不到任何关于做这件事的信息,我想一定有人以前做过。我不知道有任何教程涉及这一主题,但不久前,在其他人的帮助下,我完成了这项工作。下面是.h和.m文件中的全部代码。我可以解释整件事,但每一行都有评论,所以我想这已经足够清楚了 h
我希望以上内容足够清晰和有用,如果您需要帮助,请告诉我。从这里下载完整的源代码 或
您好@XCode Monkey。我正在从服务器获取数据并在tableview中显示。我必须将textfield实现为搜索栏并在该textfield中搜索。我已尝试了您的代码,但无法获取。请帮助me@AppDeveloper,由于您看到了代码,因此很难提供帮助。上面的代码是为一个简单数组设置的,该数组的成员在同一个类上声明。它可能与您用于从服务器提取数据的方法有关。-(BOOL)textField:(UITextField*)textField shouldChangeCharactersRange:(NSRange)range replacementString:(NSString*)string{NSPredicate*谓词=[NSPredicate predicateWithFormat:@“strSubCatName包含[cd]@”,textField.text];searray=[NSMutableArray arrayWithArray:[arrSubCatOb filteredArrayUsingPredicate:predicate]];NSLog(@“搜索数组为%@”,searchArray);[self.tableViewForSubCat重载数据];返回YES;}我正在使用此代码使用自定义搜索文本字段筛选tableview数据您的代码非常完美,但它也会更新我的数组id。它会根据tableview索引进行更改。您能帮助我吗?谢谢您,我只做了一点小的更改,而不是我使用的循环
NSPredicate
,如果数组较大,则速度会更快。对于Swift 3,我找到了一个解决方案在这里:
#import <UIKit/UIKit.h>
@interface SEMainVC : UIViewController <UITextFieldDelegate>{
NSMutableArray *dummyArray;
NSMutableArray *searchArray;
NSString *searchTextString;
}
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;
@property (weak, nonatomic) IBOutlet UITableView *contentTableView;
- (void) setupData;
@end
@interface SEMainVC ()
@end
@implementation SEMainVC
- (void)viewDidLoad
{
[super viewDidLoad];
//set the selector to the text field in order to change its value when edited
[self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
//here you set up the methods to search array and reloading the tableview
[self setupData];
[self updateSearchArray];
[self.contentTableView reloadData];
}
//setting up the data sourch for the mutable array
- (void) setupData {
dummyArray = [[NSMutableArray alloc] init];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy 1", @"name" , @"image1.JPG", @"image" , @"dummy 1 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy 2", @"name" , @"image1.JPG", @"image" , @"dummy 2 description textview", @"description", nil]];
[dummyArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"dummy 3", @"name" , @"image1.JPG", @"image" , @"dummy 3 description textview", @"description", nil]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [searchArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if( cell == nil ){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[searchArray objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"DummyDetail" sender:[NSNumber numberWithInt:indexPath.row]];
}
#pragma mark - Search Methods
-(void)textFieldDidChange:(UITextField*)textField
{
searchTextString = textField.text;
[self updateSearchArray];
}
//update seach method where the textfield acts as seach bar
-(void)updateSearchArray
{
if (searchTextString.length != 0) {
searchArray = [NSMutableArray array];
for ( NSDictionary* item in dummyArray ) {
if ([[[item objectForKey:@"name"] lowercaseString] rangeOfString:[searchTextString lowercaseString]].location != NSNotFound) {
[searchArray addObject:item];
}
}
} else {
searchArray = dummyArray;
}
[self.contentTableView reloadData];
}
#pragma mark - Table view delegate
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSNumber*)indexNumber
{
if([[segue identifier] isEqualToString:@"DummyDetail"]){
NSInteger index = [indexNumber integerValue];
SEDetailVC *dummyDetail = [segue destinationViewController];
dummyDetail.dummyImageString = [[searchArray objectAtIndex:index] objectForKey:@"image"];
dummyDetail.dummyTextString = [[searchArray objectAtIndex:index] objectForKey:@"description"];
dummyDetail.title = [[searchArray objectAtIndex:index] objectForKey:@"name"];
}
}
- (void)viewDidUnload {
[self setSearchTextField:nil];
[self setContentTableView:nil];
[super viewDidUnload];
}
@end
#import "ViewController.h"
@interface ViewController()
<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
NSMutableArray *arrOfColor;
NSMutableArray *searchArray;
NSString *searchTextString;
BOOL isFilter;
}
@property (strong, nonatomic) IBOutlet UITextField *searchTextField;
@property (strong, nonatomic) IBOutlet UITableView *colorTableview;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrOfColor=[NSMutableArray arrayWithObjects:@"Red",@"Green",@"Blue",@"Gray",@"Black",@"White",@"Yellow",@"Brown",@"Pink",nil];
[self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFilter)
{
return [searchArray count];
}
else
return [arrOfColor count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if(isFilter)
{
cell.textLabel.text=[searchArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text=[arrOfColor objectAtIndex:indexPath.row];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isFilter)
{
_searchTextField.text=[searchArray objectAtIndex:indexPath.row];
}
else
{
_searchTextField.text=[arrOfColor objectAtIndex:indexPath.row];
}
}
-(void)textFieldDidChange:(UITextField *)textField
{
searchTextString=textField.text;
[self updateSearchArray:searchTextString];
}
-(void)updateSearchArray:(NSString *)searchText
{
if(searchText.length==0)
{
isFilter=NO;
}
else
{
isFilter=YES;
searchArray=[[NSMutableArray alloc]init];
for(NSString *string in arrOfColor){
NSRange stringRange=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location !=NSNotFound){
[searchArray addObject:string];
}
}
[self.colorTableview reloadData];}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end