自定义股票搜索和股票键盘(实用干货)

如有技术问题可扣扣交流:907392876

现在能搜到的自定义股票搜索和股票键盘少之又少,本人写了一套自定义股票搜索和股票键盘的demo,可以直接用到项目中,先上效果图,如需要demo请加qq:907392876

图1


自定义股票搜索和股票键盘(实用干货)_第1张图片

图2


自定义股票搜索和股票键盘(实用干货)_第2张图片

图3


自定义股票搜索和股票键盘(实用干货)_第3张图片

2.下面是核心代码

#import"ViewController.h"

#import"PYSearch.h"

#import"PYTempViewController.h"//搜索点击跳转的控制器

#import"FMDB.h"//数据库

#import"SysKeyboardView.h"

#import"GCDAsyncSocket.h"//scoket长连接协议

@interfaceViewController()

@property(nonatomic,strong)FMDatabase*db;

@property(weak,nonatomic)IBOutletUITextField*tx;

//截取字符串

@property(nonatomic,strong)NSString*string1;

//定义scoket

@property(atomic,strong)GCDAsyncSocket*socket;

@property(nonatomic,strong)NSMutableData*responseData;

@property(nonatomic,strong)NSDictionary*rsData;

@property(nonatomic,strong)NSMutableArray*SData;

@property(nonatomic,strong)NSMutableArray*tempArray;//股票数据数组

@property(nonatomic,strong)NSMutableArray*typeArray;//股票类型

@property(nonatomic,strong)NSMutableArray*tempArray1;//流值

@property(nonatomic,strong)NSMutableArray*ceshiArray1;//测试用

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

//创建数据库

[selfdataBase];

//查询

//[self query];

//搜索

//弹出键盘

//[SysKeyboardView directToReverseTextFiledInputView:self.tx];

//连接服务器进行接口登录

[selfsign];

}

//连接服务器进行接口登录方

- (void)sign{

[selfconnectToServer];

[selfsentToScoket:0withCode:@"13984054154"withCodetype:@"123"withHqtype:@(1)withFqtype:@(1)withStartdate:@(1)withEnddate:@(1)];

}

//断开scoket

- (void)cutScoket{

[self.socketdisconnect];

}

//1.连接服务器

- (void)connectToServer{

// 1.与服务器通过三次握手建立连接

//公司服务器IP

NSString*host =@"192.168.1.199";

//服务器端口

intport =8;

//2.创建一个socket对象

_socket= [[GCDAsyncSocketalloc]initWithDelegate:selfdelegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)];

//3.连接服务器

NSError*error =nil;

[_socketconnectToHost:hostonPort:porterror:&error];

if(error) {

NSLog(@"%@",error);

NSLog(@"连接不成功");

}

}

//发送参数

//1.第一次- (void)sentToScoket:(NSNumber *)datatype withCode:(NSString *)code withCodetype:(NSString *)codetype withHqtype:(NSNumber *)hqtype withStartdate:(NSNumber *)startdate withEnddate:(NSNumber *)enddate{

//2.有复权请求- (void)sentToScoket:(NSNumber *)datatype withCode:(NSString *)code withCodetype:(NSString *)codetype withHqtype:(NSNumber *)hqtype withFqtype:(NSNumber *)fqtype withStartdate:(NSNumber *)startdate withEnddate:(NSNumber *)enddate{

- (void)sentToScoket:(NSNumber*)datatype withCode:(NSString*)code withCodetype:(NSString*)codetype withHqtype:(NSNumber*)hqtype withFqtype:(NSNumber*)fqtype withStartdate:(NSNumber*)startdate withEnddate:(NSNumber*)enddate{

/*

//1.每次启动发送的固定参数

//NSDictionary * dict = @{@"headid":@(0x0139),

//@"version":@(0x1000),

//@"dataid":@(132),

//@"datatype":@(2),

//@"hqtype":@(12),

//@"blockcode":@"0"};

//1.每次启动发送的固定参数(获取所有股票代码)

//NSMutableDictionary *dict = [NSMutableDictionary dictionary];

//dict[@"mobiletype"] = @(0);

//dict[@"headid"] = @(0x0139);

//dict[@"version"] = @(0x1000);

//dict[@"dataid"] = @(132);

//dict[@"datatype"] = @(2);

//dict[@"hqtype"] = @(12);

//dict[@"blockcode"] = @"0";

//2.发送实时行情参数

//NSMutableDictionary *dict = [NSMutableDictionary dictionary];

//dict[@"mobiletype"] = @(0);

//dict[@"headid"] = @(0x0139);

//dict[@"version"] = @(0x1000);

//dict[@"dataid"] = @(132);

////dict[@"delete"] = @(1);

//dict[@"datatype"] = @(2);

//dict[@"code"] = @"603999";

//dict[@"isblock"] = @(1);

//dict[@"hqtype"] = @(0);

//dict[@"hqcol"] = @"0,1,2,3,4,10,11,12,13,14";

//3.发送列表行情参数

//NSMutableDictionary *dict = [NSMutableDictionary dictionary];

//dict[@"mobiletype"] = @(0);

//dict[@"headid"] = @(0x0139);

//dict[@"version"] = @(0x1000);

//dict[@"dataid"] = @(132);

////dict[@"delete"] = @(1);

//dict[@"datatype"] = @(2);

//dict[@"hqtype"] = @(13);

//dict[@"hqcol"] = @"0,1,2,10";

//dict[@"sortcol"] = @(-20);

//dict[@"count"] = @"1,10";

//4.发送K线行情参数

//NSMutableDictionary *dict = [NSMutableDictionary dictionary];

//dict[@"mobiletype"] = @(0);

//dict[@"headid"] = @(0x0139);

//dict[@"version"] = @(0x1000);

//dict[@"dataid"] = @(132);

////dict[@"delete"] = @(1);

//dict[@"datatype"] = @(2);

////dict[@"code"] = @"000001";

//dict[@"code"] = @"603000";

//dict[@"codetype"] = @"0";

//dict[@"hqtype"] = @(1);

//dict[@"startdate"] = @(20150912);

//dict[@"enddate"] = @(20161101);

//5.发送分时行情参数(上证)

//NSMutableDictionary *dict = [NSMutableDictionary dictionary];

//dict[@"mobiletype"] = @(0);

//dict[@"headid"] = @(0x0139);

//dict[@"version"] = @(0x1000);

//dict[@"dataid"] = @(132);

////dict[@"delete"] = @(1);

//dict[@"datatype"] = @(2);

//dict[@"code"] = @"000001";

////dict[@"code"] = @"603999";

//// dict[@"isblock"] = @(1);

//dict[@"codetype"] = @"0";

//dict[@"hqtype"] = @(11);

//dict[@"startdate"] = @(20161012);

//dict[@"enddate"] = @(20161012);

////发送K线行情参数

//NSMutableDictionary *dict = [NSMutableDictionary dictionary];

//dict[@"mobiletype"] = @(0);

//dict[@"headid"] = @(0x0139);

//dict[@"version"] = @(0x1000);

//dict[@"dataid"] = @(132);

//dict[@"datatype"] = datatype;//请求类型

//dict[@"code"] = code;//股票代码

//dict[@"codetype"] = codetype;//代码类型

//dict[@"hqtype"] = hqtype;//行情类型

//dict[@"fqtype"] = fqtype;//复权(可选)

//dict[@"startdate"] =startdate;//开始时间(可选)

//dict[@"enddate"] = enddate;//结束时间(可选)

////dict[@"hqnums"] = @(300);;//请求的数据(可选)

*/

////发送多个参数

//NSMutableDictionary *dict = [NSMutableDictionary dictionary];

//dict[@"mobiletype"] = @(0);

//dict[@"headid"] = @(0x0139);

//dict[@"version"] = @(0x1000);

//dict[@"reqarray"] = @[

//@{

//@"dataid": @(133),

//@"datatype" : @(2),

//@"code": @"000001",

//@"codetype" : @"0",

//@"hqtype": @(1),

//@"fqtype": @(1),

//@"hqcol": @""

//},

//@{

//@"dataid": @(134),

//@"datatype" : @(2),

//@"code": @"603000",

//@"codetype" : @"0",

//@"hqtype": @(0),

//@"hqcol": @"36,37,42",

//@"fqtype": @(0)

//},

//

//@{

//@"dataid": @(136),

//@"datatype" : @(2),

//@"code": @"603999",

//@"codetype" : @"0",

//@"hqtype": @(0),

//@"hqcol": @"36,37,42",

//@"fqtype": @(0)

//}

//];

//登录接口

NSMutableDictionary*dict = [NSMutableDictionarydictionary];

dict[@"mobiletype"] =@(0);

dict[@"headid"] =@(0x0139);

dict[@"version"] =@(0x1000);

dict[@"reqarray"] =@[

@{

@"dataid":@(133),

@"datatype":@(0),

@"userid":@"13984054154",

@"codenum":@(11632),

@"lasttime":@(20161105)

}

];

//字典转json字符串

NSString*jsonStr = [selftoJSONWithString:dict];

NSData* jsData = [jsonStrdataUsingEncoding:NSUTF8StringEncoding];

//发送数据

[self.socketwriteData:jsDatawithTimeout:1tag:1];

}

//字典转json字符串

- (NSString*)toJSONWithString:(NSDictionary*)dict

{

NSError*error;

//字典转json

NSData*jsonData = [NSJSONSerializationdataWithJSONObject:dictoptions:NSJSONWritingPrettyPrintederror:&error];

//转json字符串

NSString*str = [[NSStringalloc]initWithData:jsonDataencoding:NSUTF8StringEncoding];

//拼接结束符

str = [strstringByAppendingString:@"\0"];

str = [NSStringstringWithFormat:@"%@",str];

returnstr;

}

#pragma mark -socket的代理

#pragma mark连接成功调用该方法

-(void)socket:(GCDAsyncSocket*)sock didConnectToHost:(NSString*)host port:(uint16_t)port{

NSLog(@"连接成功");

}

#pragma mark断开连接时候执行

-(void)socketDidDisconnect:(GCDAsyncSocket*)sock withError:(NSError*)err{

if(err) {

NSLog(@"连接失败");

}else{

NSLog(@"正常断开");

}

}

#pragma mark数据发送成功

-(void)socket:(GCDAsyncSocket*)sock didWriteDataWithTag:(long)tag{

NSLog(@"*********发送成功*********");

//发送完数据手动读取,-1不设置超时(-1代表不超时,tag标示作用)

[sockreadDataWithTimeout:-1tag:tag];

}

#pragma mark读取数据

- (void)socket:(GCDAsyncSocket*)sock didReadData:(NSData*)data withTag:(long)tag

{

//json数据转成字符串

NSString*receiverStr = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];

if(!self.responseData) {

self.responseData= [NSMutableDatadata];

}

//股票类型数组

if(!self.typeArray) {

self.typeArray= [NSMutableArrayarray];

}

//133股票数据数组

if(!self.tempArray) {

self.tempArray= [NSMutableArrayarray];

}

//134流值

if(!self.tempArray1) {

self.tempArray1= [NSMutableArrayarray];

}

//测试用

if(!self.ceshiArray1) {

self.ceshiArray1= [NSMutableArrayarray];

}

if(![receiverStrhasSuffix:@"15:00"]) {

[self.responseDataappendData:data];

//json数据转成字典

NSError*err;

NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:self.responseDataoptions:NSJSONReadingMutableContainerserror:&err];

_rsData= dic;

//if(!err)

//{

//////股票类型

////[self.typeArray addObjectsFromArray:dic[@"codearray"]];

//////股票数据

////if (self.tempArray.lastObject[@"time"] ==dic[@"codearray"][0][@"hqinfo"][0][@"time"] ) {

////[self.tempArray removeLastObject];

////}

////

////[self.tempArray addObjectsFromArray:dic[@"codearray"][0][@"hqinfo"] ];

////// NSLog(@"00000000000000000:%@",self.tempArray);

//

//if ([dic[@"dataid"] integerValue] == 133) {

//

//if (self.tempArray.lastObject[@"time"] ==dic[@"codearray"][0][@"hqinfo"][0][@"time"] ) {

//[self.tempArray removeLastObject];

//}

////股票类型

//[self.typeArray addObjectsFromArray:dic[@"codearray"]];

//

//dispatch_sync(dispatch_get_main_queue(), ^{

////判断进行是否复权

//[self rights:self.typeArray];

//});

//

////股票数据

//[self.tempArray addObjectsFromArray:dic[@"codearray"][0][@"hqinfo"] ];

//

//}else if ([dic[@"dataid"] integerValue] == 134){

////股票数据

//[self.tempArray1 addObjectsFromArray:dic[@"hqinfo"]];

//}else if ([dic[@"dataid"] integerValue] == 136){

////股票数据

//[self.ceshiArray1 addObjectsFromArray:dic[@"hqinfo"]];

//}

//

////配置数据源,生成K线图

////dispatch_async(dispatch_get_main_queue(), ^{//串行

//dispatch_sync(dispatch_get_main_queue(), ^{//并行

////复权类型

////[self rights:self.typeArray];

////画K线

//[self loadLocalCandleStickData:DisplayDailyType with:self.tempArray];

////市盈率流值赋值

//[self flowValue:self.tempArray1];

////测试用

//[self ceshi:self.ceshiArray1];

//self.responseData = nil;

//});

//

////将字典保存到plist文件中

//[dic writeToFile:@"/Users/vsatsh/Desktop/mart00.plist" atomically:YES];

////self.responseData = nil;

//

//}else {

//NSLog(@"json数据转成字典失败:%@",err);

//}

}

//NSLog(@"-----------以下为读取的数据------------");

NSLog(@"*************receiverStr:%@**********",receiverStr);

//开启读取数据,为了下次能接受数据

[sockreadDataWithTimeout:-1tag:tag];

NSLog(@"接收收据线程:%@",[NSThreadcurrentThread]);

}

//创建数据库

- (void)dataBase{

//// 0.拼接数据库存放的沙盒路径

//NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;

////表的路径

//// NSString *sqlFilePath = [path stringByAppendingPathComponent:@"student.sqlite"];

//NSString *sqlFilePath = [path stringByAppendingPathComponent:@"clientcode(2).sqlite"];

//

//NSLog(@"%@",sqlFilePath);

NSString*sqlFilePath = [[NSBundlemainBundle]pathForResource:@"clientcode(2).sqlite"ofType:nil];

// NSLog(@"----%@",sqlFilePath);

// 1.通过路径创建数据库

self.db= [FMDatabasedatabaseWithPath:sqlFilePath];

// 2.打开数据库

if([self.dbopen]) {

NSLog(@"打开成功");

BOOLsuccess = [self.dbexecuteUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER DEFAULT 1)"];

if(success) {

NSLog(@"创建表成功");

}else{

NSLog(@"创建表失败");

}

}else{

NSLog(@"打开失败");

}

}

- (IBAction)click:(id)sender {

// 1.创建热门搜索

//NSArray *hotSeaches = @[@"Java", @"Python", @"Objective-C", @"Swift", @"C", @"C++", @"PHP", @"C#", @"Perl", @"Go", @"JavaScript", @"R", @"Ruby", @"MATLAB"];

// 2.创建控制器

PYSearchViewController*searchViewController = [PYSearchViewControllersearchViewControllerWithHotSearches:nilsearchBarPlaceholder:@"请输入个股代码,或简拼"didSearchBlock:^(PYSearchViewController*searchViewController,UISearchBar*searchBar,NSString*searchText) {

//开始搜索执行以下代码

//如:跳转到指定控制器

//打印选中时搜索的searchtext

NSLog(@"打印选中:%@",searchText);

//从面开始截取到第7位

NSString*b = [searchTextsubstringFromIndex:searchText.length-7];

NSLog(@"从后面截取:%@",b);

//截取到第6位

NSString*c = [bsubstringToIndex:6];

NSLog(@"截取到第六位:%@",c);

//字符串转int

NSIntegerd = [cintegerValue];

NSLog(@"字符串转成了int类型:%ld",(long)d);

[searchViewController.navigationControllerpushViewController:[[PYTempViewControlleralloc]init]animated:YES];

}];

//// 3.设置风格

//if (indexPath.section == 0) { //选择热门搜索

//searchViewController.hotSearchStyle = (NSInteger)indexPath.row; //热门搜索风格根据选择

//searchViewController.searchHistoryStyle = PYHotSearchStyleDefault; //搜索历史风格为default

//} else { //选择搜索历史

//searchViewController.hotSearchStyle = PYHotSearchStyleDefault; //热门搜索风格为默认

//searchViewController.searchHistoryStyle = (NSInteger)indexPath.row; //搜索历史风格根据选择

//}

// 4.设置代理

searchViewController.delegate=self;

// 5.点击按钮跳转到搜索控制器

UINavigationController*nav = [[UINavigationControlleralloc]initWithRootViewController:searchViewController];

[selfpresentViewController:navanimated:NOcompletion:nil];

}

///**搜索框文本变化时,显示的搜索建议通过searchViewController的searchSuggestions赋值即可*/

- (void)searchViewController:(PYSearchViewController*)searchViewController searchTextDidChange:(UISearchBar*)seachBar searchText:(NSString*)searchText//searchText:搜索输入的值

{

//NSLog(@"----%@",searchText);

//NSLog(@"%@",searchText);

/*

//直接调用这个方法就行(辨别字符串中的拼音和数字)

-(int)checkIsHaveNumAndLetter:(NSString*)password{

//数字条件

NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];

//符合数字条件的有几个字节

NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:password

options:NSMatchingReportProgress

range:NSMakeRange(0, password.length)];

//英文字条件

NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];

//符合英文字条件的有几个字节

NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

if (tNumMatchCount == password.length) {

//全部符合数字,表示沒有英文

return 1;

} else if (tLetterMatchCount == password.length) {

//全部符合英文,表示沒有数字

return 2;

} else if (tNumMatchCount + tLetterMatchCount == password.length) {

//符合英文和符合数字条件的相加等于密码长度

return 3;

} else {

return 4;

//可能包含标点符号的情況,或是包含非英文的文字,这里再依照需求详细判断想呈现的错误

}

}

*/

//以某个字符串开头

// NSString *string = [NSString stringWithFormat:@"SELECT * FROM T_Code WHERE code LIKE '%@%%'",searchText];

//包含有某个字符串

// NSString *string = [NSString stringWithFormat:@"SELECT * FROM T_Code WHERE code LIKE '%%%@%%'",searchText];

//以某个字符串结尾

// NSString *string = [NSString stringWithFormat:@"SELECT * FROM T_Code WHERE code LIKE '%%%@'",searchText];

if(searchText.length) {//与搜索条件再搜索

//根据条件发送查询(这里模拟搜索)

//判断是英文字符串还是数字字符串

//数字条件

NSRegularExpression*tNumRegularExpression = [NSRegularExpressionregularExpressionWithPattern:@"[0-9]"options:NSRegularExpressionCaseInsensitiveerror:nil];

//符合数字条件的有几个字节

NSUIntegertNumMatchCount = [tNumRegularExpressionnumberOfMatchesInString:searchTextoptions:NSMatchingReportProgressrange:NSMakeRange(0, searchText.length)];

//英文字条件

NSRegularExpression*tLetterRegularExpression = [NSRegularExpressionregularExpressionWithPattern:@"[A-Za-z]"options:NSRegularExpressionCaseInsensitiveerror:nil];

//符合英文字条件的有几个字节

NSUIntegertLetterMatchCount = [tLetterRegularExpressionnumberOfMatchesInString:searchTextoptions:NSMatchingReportProgressrange:NSMakeRange(0, searchText.length)];

if(tNumMatchCount == searchText.length) {//全部符合数字,表示沒有英文

//代码(数字)

NSString*string = [NSStringstringWithFormat:@"SELECT * FROM T_Code WHERE code LIKE '%%%@%%'",searchText];

_string1= string;

}elseif(tLetterMatchCount == searchText.length) {//全部符合英文,表示沒有数字

//名字(英文)

NSString*string = [NSStringstringWithFormat:@"SELECT * FROM T_Code WHERE name LIKE '%%%@%%'",searchText];

_string1= string;

}elseif(tNumMatchCount + tLetterMatchCount == searchText.length) {//符合英文和符合数字条件的相加等于密码长度

// NSLog(@"%lu",(unsigned long)searchText.length);

}else{//可能包含标点符号的情況,或是包含非英文的文字,这里再依照需求详细判断想呈现的错误

//NSLog(@"输入格式不正确,请输入个股代码或者名称简拼");

}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{//搜素完毕

// 1.执行查询语句

//时间

//FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM T_UpdateTime"];

//股票及代码SELECT * FROM Students WHERE Name LIKE '小_'

FMResultSet*resultSet = [self.dbexecuteQuery:_string1];//SELECT * FROM T_Code LIKE '9'

NSMutableArray*searchSuggestionsM = [NSMutableArrayarray];

// 2.遍历结果

while([resultSetnext]) {

//股票代码

NSString*code = [resultSetstringForColumn:@"code"];

//股票类型

NSString*codetype= [resultSetstringForColumn:@"codetype"];

//名字

NSString*name = [resultSetstringForColumn:@"name"];

//有类型

//NSString *all = [NSString stringWithFormat:@"%@ %@(%@)",codetype,name,code];

//无类型

NSString*all = [NSStringstringWithFormat:@" %@(%@)",name,code];

[searchSuggestionsMaddObject:all];

//NSLog(@"值:%@ %@ %@", code,codetype, name);

////时间

//NSString *time = [resultSet stringForColumn:@"time"];

//NSLog(@"时间:%@ ", time);

}

//返回(底部显示的搜索建议)

searchViewController.searchSuggestions= searchSuggestionsM;

});

}

}

//数据库查询

- (void)query{

////接收更新的sql语句

//BOOL success = [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES ;"];

//if (success) {

//NSLog(@"创建表成功");

//} else {

//NSLog(@"创建表失败");

////接收更新的sql语句,如果更新失败重新更新

//[self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES ;"];

//

//}

//

// 1.执行查询语句

FMResultSet*resultSet = [self.dbexecuteQuery:@"SELECT * FROM T_Code"];

// 2.遍历结果

while([resultSetnext]) {

//id表示前面的序号

//int ID = [resultSet intForColumn:@"id"];

//int code = [resultSet intForColumn:@"code"];

NSString*code = [resultSetstringForColumn:@"code"];

//名字

NSString*name = [resultSetstringForColumn:@"name"];

//年龄

// int age = [resultSet intForColumn:@"age"];

// NSLog(@"22222222222222 %@ %@", code, name);

}

}

@end

你可能感兴趣的:(自定义股票搜索和股票键盘(实用干货))