IphoneBIMe/IphoneBIMe/Classes/ProjectList/Views/PBProjectSearchView.m
@@ -7,15 +7,18 @@
//
#import "PBProjectSearchView.h"
#import "MSSAutoresizeLabelFlow.h"
#import "PBProjectTableViewCell.h"
#import "PBProjectModel.h"
#import "PBTabBarController.h"
@interface PBProjectSearchView()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,strong)MSSAutoresizeLabelFlow *historicalRecordV;
#import "LBKeyWordLayout.h"
#import "KeyWordCell.h"
@interface PBProjectSearchView()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, weak) UISearchBar *searchBar;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (strong, nonatomic) LBKeyWordLayout *layout;
@property (strong, nonatomic) NSArray *items;
@end
@implementation PBProjectSearchView
- (instancetype)initWithFrame:(CGRect)frame {
@@ -87,13 +90,21 @@
        make.right.equalTo(self).offset(-18);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    NSArray *array = [self readSearchHistory];
    _historicalRecordV = [[MSSAutoresizeLabelFlow alloc] initWithFrame:CGRectMake(0, 150, PBScreenWidth, 200) titles:array selectedHandler:^(NSUInteger index, NSString *title) {
        NSLog(@"%@",title);
        self.searchBar.text = title;
        [self searchActionWithTitle:title];
    }];
    [self addSubview:_historicalRecordV];
    self.items = [self readSearchHistory];
//    _historicalRecordV = [[MSSAutoresizeLabelFlow alloc] initWithFrame:CGRectMake(0, 150, PBScreenWidth, 200) titles:array selectedHandler:^(NSUInteger index, NSString *title) {
//        NSLog(@"%@",title);
//        self.searchBar.text = title;
//        [self searchActionWithTitle:title];
//    }];
    self.layout = [[LBKeyWordLayout alloc] init];
    self.layout.estimatedItemSize = CGSizeMake(80, 30);
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, PBScreenWidth, 400) collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[KeyWordCell class] forCellWithReuseIdentifier:@"KeyWordCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    [self addSubview:self.tableView];
    self.tableView.hidden = YES;
}
@@ -110,7 +121,8 @@
    }
    [arrM insertObject:title atIndex:0];
    [self writeSearchHistoryWithData:arrM.copy];
    [self.historicalRecordV reloadAllWithTitles:arrM.copy];
    self.items = arrM.copy;
    [self.collectionView reloadData];
}
- (void)backActon {
    if (self.cancelSearchBlock) {
@@ -219,7 +231,8 @@
    NSString *documentPath = [documents stringByAppendingPathComponent:@"searchHistory.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:documentPath error:nil];
    [self.historicalRecordV reloadAllWithTitles:@[]];
    self.items = @[];
    [self.collectionView reloadData];
}
#pragma mark - 插入本地历史记录
- (void)writeSearchHistoryWithData:(NSArray *)history {
@@ -228,6 +241,23 @@
    NSString *documentPath = [documents stringByAppendingPathComponent:@"searchHistory.plist"];
    [history writeToFile:documentPath atomically:YES];
}
#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KeyWordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KeyWordCell" forIndexPath:indexPath];
    cell.maxWidthConstraint.constant = CGRectGetWidth(collectionView.bounds) - self.layout.sectionInset.left - self.layout.sectionInset.right - cell.layoutMargins.left - cell.layoutMargins.right - 10;
    cell.textLabel.numberOfLines = 1;
    cell.textLabel.text = self.items[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.searchBar.text = self.items[indexPath.row];
    [self searchActionWithTitle:self.items[indexPath.row]];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.