CoreSpotlight 系统搜索栏表格内容搜索
2016-10-24 15:08
274 查看
1.首先#import <CoreSpotlight/CoreSpotlight.h> 2.创建表实现内容添加 3.AppDelegate回调
2-----
<UITableViewDelegate,
UITableViewDataSource>
@property(nonatomic,
strong)NSMutableArray *friendArray; //表格数据模型
@property(nonatomic,
strong)UITableView *tableView;
- (void)viewDidLoad {
[super
viewDidLoad];
[self
friendArray];
[self.view
addSubview:self.tableView];
[self
saveFriend];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return
self.friendArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString *identf = @"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identf
forIndexPath:indexPath];
Friend *frinde = (Friend *)[_friendArray
objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage
imageNamed:frinde.image];
cell.textLabel.text =frinde.name;
cell.detailTextLabel.text = frinde.address;
NSLog(@"%@", frinde.address);
return cell;
}
-(UITableView *)tableView{
if ( !_tableView) {
_tableView = [[UITableView
alloc] initWithFrame:CGRectMake(0,
20, self.view.bounds.size.width,
self.view.bounds.size.height)];
_tableView.delegate =
self;
_tableView.dataSource =self;
[_tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"cell"];
}
return
_tableView;
}
- (NSMutableArray *)friendArray{
if (!_friendArray) {
_friendArray = [[NSMutableArray
alloc] init];
NSArray *array =
@[@"aaaa",
@"bbbb", @"ccccc",@"abcd"];
int i =
0;
for (NSString *item
in array) {
Friend *friend = [[Friend
alloc] init];
friend.name = item;
friend.image = [NSString
stringWithFormat:@"%d.jpg", ++i];
friend.f_id = [NSString
stringWithFormat:@"%d", i];
friend.address = item;
[_friendArray
addObject:friend];
}
}
return (_friendArray !=
nil)?_friendArray :
nil;
}
-(void)saveFriend{
NSMutableArray <CSSearchableItem *> *seatrchchbleItem = [NSMutableArray
array];
//将Friend所有属性转换成CSSearchableItem类型
for (Friend *friend
in _friendArray) {
CSSearchableItemAttributeSet *attritable = [[CSSearchableItemAttributeSet
alloc] initWithItemContentType:@"image"];
attritable.title = friend.name;
// attritable.contentDescription = friend.webUrl;
attritable.thumbnailData =
UIImagePNGRepresentation([UIImage
imageNamed:friend.image])
;
CSSearchableItem *item = [[CSSearchableItem
alloc] initWithUniqueIdentifier:friend.f_id
domainIdentifier:@"www.baidu.com"
attributeSet:attritable];
[seatrchchbleItem addObject:item];
}
//将属性注入到设置中
[[CSSearchableIndex
defaultSearchableIndex] indexSearchableItems:seatrchchbleItem
completionHandler:^(NSError *
_Nullable error) {
if (error !=
nil) {
NSLog(@" error = %@",error);
}
}];
}
-(void)loadImage:(NSString *)f_id{
Friend *someFriend =
nil;
for (Friend *item
in _friendArray) {
if ([item.f_id
isEqualToString:f_id]) {
someFriend = item;
break;
}
}
if (someFriend) {
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:CGRectMake(150,
300, 50,
50)];
imageView.image = [UIImage
imageNamed:someFriend.image];
[self.view
addSubview:imageView];
}
}
3----
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity
*)userActivity restorationHandler:(void (^)(NSArray *
_Nullable))restorationHandler{
NSLog(@"被调用...");
NSString *f_id = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
ViewController *vc = nav.viewControllers.firstObject;
[vc loadImage:f_id];
return
true;
}
2-----
<UITableViewDelegate,
UITableViewDataSource>
@property(nonatomic,
strong)NSMutableArray *friendArray; //表格数据模型
@property(nonatomic,
strong)UITableView *tableView;
- (void)viewDidLoad {
[super
viewDidLoad];
[self
friendArray];
[self.view
addSubview:self.tableView];
[self
saveFriend];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return
self.friendArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
static
NSString *identf = @"cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identf
forIndexPath:indexPath];
Friend *frinde = (Friend *)[_friendArray
objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage
imageNamed:frinde.image];
cell.textLabel.text =frinde.name;
cell.detailTextLabel.text = frinde.address;
NSLog(@"%@", frinde.address);
return cell;
}
-(UITableView *)tableView{
if ( !_tableView) {
_tableView = [[UITableView
alloc] initWithFrame:CGRectMake(0,
20, self.view.bounds.size.width,
self.view.bounds.size.height)];
_tableView.delegate =
self;
_tableView.dataSource =self;
[_tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"cell"];
}
return
_tableView;
}
- (NSMutableArray *)friendArray{
if (!_friendArray) {
_friendArray = [[NSMutableArray
alloc] init];
NSArray *array =
@[@"aaaa",
@"bbbb", @"ccccc",@"abcd"];
int i =
0;
for (NSString *item
in array) {
Friend *friend = [[Friend
alloc] init];
friend.name = item;
friend.image = [NSString
stringWithFormat:@"%d.jpg", ++i];
friend.f_id = [NSString
stringWithFormat:@"%d", i];
friend.address = item;
[_friendArray
addObject:friend];
}
}
return (_friendArray !=
nil)?_friendArray :
nil;
}
-(void)saveFriend{
NSMutableArray <CSSearchableItem *> *seatrchchbleItem = [NSMutableArray
array];
//将Friend所有属性转换成CSSearchableItem类型
for (Friend *friend
in _friendArray) {
CSSearchableItemAttributeSet *attritable = [[CSSearchableItemAttributeSet
alloc] initWithItemContentType:@"image"];
attritable.title = friend.name;
// attritable.contentDescription = friend.webUrl;
attritable.thumbnailData =
UIImagePNGRepresentation([UIImage
imageNamed:friend.image])
;
CSSearchableItem *item = [[CSSearchableItem
alloc] initWithUniqueIdentifier:friend.f_id
domainIdentifier:@"www.baidu.com"
attributeSet:attritable];
[seatrchchbleItem addObject:item];
}
//将属性注入到设置中
[[CSSearchableIndex
defaultSearchableIndex] indexSearchableItems:seatrchchbleItem
completionHandler:^(NSError *
_Nullable error) {
if (error !=
nil) {
NSLog(@" error = %@",error);
}
}];
}
-(void)loadImage:(NSString *)f_id{
Friend *someFriend =
nil;
for (Friend *item
in _friendArray) {
if ([item.f_id
isEqualToString:f_id]) {
someFriend = item;
break;
}
}
if (someFriend) {
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:CGRectMake(150,
300, 50,
50)];
imageView.image = [UIImage
imageNamed:someFriend.image];
[self.view
addSubview:imageView];
}
}
3----
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity
*)userActivity restorationHandler:(void (^)(NSArray *
_Nullable))restorationHandler{
NSLog(@"被调用...");
NSString *f_id = userActivity.userInfo[@"kCSSearchableItemActivityIdentifier"];
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
ViewController *vc = nav.viewControllers.firstObject;
[vc loadImage:f_id];
return
true;
}
相关文章推荐
- jquery 表格排序,实时搜索表格内容
- (32)DOM应用之搜索表格中的内容(忽略大小写搜索)
- asp.net JQuery Ajax WebService 搜索 自动匹配 动态生成表格 点击表格 获取表格内容
- (33)DOM应用之搜索表格中的内容(模糊搜索)
- JavaScript document 对表格内容搜索 模糊搜索 多个关键字搜索
- angular.js表格内容搜索过滤代码
- jquery 表格排序、实时搜索表格内容(附图)
- (31)DOM应用之搜索表格中的内容(简单搜索)
- Win10系统关闭搜索时的网页内容提示的设置教程
- 【牛腩新闻发布系统】改变表格的内容
- DEDECMS织梦出现,你的搜索关键字中存在非法内容,被系统禁止!
- JS实现table表格内针对某列内容进行即时搜索筛选功能
- jquery 表格排序、实时搜索表格内容(附图)
- (34)DOM应用之搜索表格中的内容(多关键字搜索)
- 4、表格内容搜索
- 基于WEBSERVICE的课件搜索系统(二)
- 将Datagrid或其余表格控件的内容导入到Excel中
- 内容管理系统(CMS)的设计和选型
- 基于WEBSERVICE的课件搜索系统(二)
- 根据传入的recordset对象实例,按表格输出内容子过程