ios - 文本字段作为搜索栏

标签 ios objective-c search uitextfield uisearchbar

有什么好的教程或方法可以将文本字段变成搜索栏吗?

我已经用搜索栏 + 搜索显示 Controller 设置了我的搜索栏,它工作正常——但很难定制……特别是摆脱难看的灰色覆盖,显然这是最好的方法。

我一直没能找到任何关于执行此操作的信息,并且认为之前必须有人这样做过。

最佳答案

我不知道有任何教程涵盖该主题,但前段时间在其他人的帮助下我能够完成该主题。这是 .h 和 .m 文件中的全部代码。我可以解释整个事情,但它在每一行都有评论,所以我想它已经足够清楚了。

.h

#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

.m

@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

我希望以上内容足够清楚和有用,如果您需要任何帮助,请告诉我。

关于ios - 文本字段作为搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22437828/

相关文章:

ios - 正则表达式仅用于UITextfield中的名称

python - 搜索大型排序文本文件的最快、最有效的方法

ios - 当前方法显示空白 View Controller

objective-c - 使用 "id"作为自定义 "init"方法的返回类型而不是指向该类的指针的优点和缺点?

ios - 由于现有项目的 Xcode 12 uitextfields 具有黑色背景

objective-c - 获取 NSIncation 消息的发送者

firefox - 更新或删除 Firefox 的自定义搜索提供程序

php - sphinx 搜索 Laravel 5.2 "Nothing to publish for tag []."

iphone - 巨大的图像显示在 iPhone 上

ios - 使用 hidesBottomBarWhenPushed 的推送动画期间工具栏定位不正确