CoreSpotlight 系统搜索栏表格内容搜索

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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值