OC 使用UISearchBar 实现简单的搜索界面

1.创建BaseSearchViewController来作为搜索的控制器:

#import <UIKit/UIKit.h>
@interface BaseSearchViewController : UIViewController
@property (nonatomic, strong)UISearchBar *searchBar;
@end
#import "BaseSearchViewController.h"

@interface BaseSearchViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>
/**结果列表*/
@property (nonatomic, strong)UITableView *resultTableView;
/**结果数据*/
@property (nonatomic, strong)NSMutableArray *resultData;
@end

@implementation BaseSearchViewController
- (void)viewWillAppear:(BOOL)animated {
    //界面出现时弹出键盘
    [self.searchBar becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
    //界面消失时隐藏键盘
    [self.searchBar resignFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, UI_SCREEN_WIDTH, HeightForNagivationBarAndStatusBar - STATUS_BAR_HEIGHT)];
    self.searchBar.delegate = self;
    //设置searchBar背景颜色
    self.searchBar.barTintColor = App_Main_Color;
    //隐藏searchBar的上下两条线
    UIImageView *imageV = [[[[self.searchBar subviews] firstObject]subviews]firstObject];
    imageV.layer.borderColor = App_Main_Color.CGColor;
    imageV.layer.borderWidth = 1;
    //显示取消按钮
    self.searchBar.showsCancelButton = YES;
    //设置占位字
    self.searchBar.placeholder = @"搜索";
    //设置取消按钮样式
    UIButton *cancelButton = [self.searchBar valueForKey:@"_cancelButton"];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    //将searchBar放到导航栏上
    self.navigationItem.titleView = self.searchBar;
    //将列表放到界面上
    [self.view addSubview:self.resultTableView];
}
#pragma mark - 结果列表
- (UITableView *)resultTableView {
    if (!_resultTableView) {
        _resultTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - HeightForNagivationBarAndStatusBar - HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
        _resultTableView.delegate = self;
        _resultTableView.dataSource = self;
        _resultTableView.backgroundColor = [UIColor whiteColor];
        _resultTableView.hidden = YES;
    }
    return _resultTableView;
}
#pragma mark - 结果数据
- (NSMutableArray *)resultData {
    if (!_resultData) {
        _resultData = [NSMutableArray array];
    }
    return _resultData;
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return self.resultData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = self.resultData[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - UISearchBarDelegate
//结束输入
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
}
//开始输入
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
}
//搜索文字发生改变
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self startSearchWithKeywords:searchText];
}
//搜索按钮被点击
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self startSearchWithKeywords:searchBar.text];
}
//取消按钮被点击
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self dismissViewControllerAnimated:NO completion:nil];
}
//开始搜索操作
- (void)startSearchWithKeywords:(NSString *)keywords {
    [self.resultData removeAllObjects];
    if (!keywords || keywords.length == 0) {
        self.resultTableView.hidden = YES;
    }else {
        [self.resultData addObject:keywords];
        self.resultTableView.hidden = NO;
    }
    [self.resultTableView reloadData];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

2.使用方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH - 120, 30)];
    textF.delegate = self;
    textF.backgroundColor = [UIColor whiteColor];
    textF.layer.cornerRadius = 15;
    textF.layer.masksToBounds = YES;
    textF.placeholder = @"点击搜索";
    self.navigationItem.titleView = textF;
}
    
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField resignFirstResponder];
     BaseSearchViewController *vc = [[BaseSearchViewController alloc]init];
     BaseNavigationController *na = [[BaseNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:na animated:NO completion:nil];
}
您可以使用CFNetwork框架来实现FTP上传图片。下面是一个简单的示例代码: ``` - (void)uploadImageToFTP:(UIImage *)image { // FTP服务器地址、用户名、密码 NSString *ftpUrl = @"ftp://ftp.example.com"; NSString *userName = @"username"; NSString *password = @"password"; // 生成图片的NSData NSData *imageData = UIImageJPEGRepresentation(image, 1.0); // 生成文件名 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]]; // 生成FTP文件路径 NSString *ftpFilePath = [NSString stringWithFormat:@"%@/%@", ftpUrl, fileName]; // 生成FTP URL CFURLRef ftpURL = CFURLCreateWithString(NULL, (__bridge CFStringRef)ftpFilePath, NULL); // 生成FTP Write Stream CFWriteStreamRef writeStream = CFWriteStreamCreateWithFTPURL(NULL, ftpURL); // 设置FTP用户名和密码 CFWriteStreamSetProperty(writeStream, kCFStreamPropertyFTPUserName, (__bridge CFStringRef)userName); CFWriteStreamSetProperty(writeStream, kCFStreamPropertyFTPPassword, (__bridge CFStringRef)password); // 打开FTP Write Stream if (CFWriteStreamOpen(writeStream)) { // 写入数据 const uint8_t *buffer = [imageData bytes]; CFIndex bytesWritten = CFWriteStreamWrite(writeStream, buffer, [imageData length]); // 关闭FTP Write Stream CFWriteStreamClose(writeStream); if (bytesWritten == [imageData length]) { NSLog(@"FTP上传成功"); } else { NSLog(@"FTP上传失败"); } } else { NSLog(@"FTP连接失败"); } // 释放资源 CFRelease(writeStream); CFRelease(ftpURL); } ``` 在上面的代码中,我们首先定义了FTP服务器地址、用户名和密码,然后生成了要上传的图片的NSData、文件名、FTP文件路径和FTP URL。接着,我们使用CFWriteStreamCreateWithFTPURL函数创建了FTP Write Stream,并设置了FTP用户名和密码。然后,我们打开FTP Write Stream,并将图片数据写入到FTP服务器上。最后,我们检查数据是否写入成功,并释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值