您的位置:首页 > 移动开发 > IOS开发

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 表内容搜索