|
//
// searchHistoryTableViewController.m
// ThePaperBase
//
// Created by Huixin on 15/7/27.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "searchHistoryCell.h"
#import "searchHistoryTableViewController.h"
#import "searchStrDB.h"
@interface searchHistoryTableViewController ()<searchHistoryCellDelegate> {
CGFloat headerHeight;
CGFloat footerHeight;
CGFloat contentHeight;
}
@property(nonatomic, strong)UIView *footerView;
@property(nonatomic, strong)UIButton *deleteAllBtn;
@property(nonatomic, strong)NSMutableArray *dataList;
@end
@implementation searchHistoryTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.tableView];
[self.tableView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeKeyBoard)];
[self.footerView addGestureRecognizer:tap];
[self.footerView addSubview:self.deleteAllBtn];
[self.deleteAllBtn makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.footerView.centerX);
make.top.equalTo(self.footerView.top).offset(25);
make.width.equalTo(@110);
make.height.equalTo(@32);
}];
}
#pragma mark - get and set method
- (UITableView*)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.autoresizingMask = AutoresizingFull;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.tableFooterView = self.footerView;
}
return _tableView;
}
- (UIView*)footerView {
if (!_footerView) {
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 82)];
_footerView.backgroundColor = [UIColor clearColor];
}
return _footerView;
}
- (UIButton*)deleteAllBtn {
if (!_deleteAllBtn) {
_deleteAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_deleteAllBtn.layer.cornerRadius = 3.0f;
_deleteAllBtn.layer.masksToBounds = YES;
_deleteAllBtn.layer.borderWidth = 1.0f;
_deleteAllBtn.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
[_deleteAllBtn setTitle:@"清除历史搜索" forState:UIControlStateNormal];
_deleteAllBtn.titleLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
[_deleteAllBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateNormal];
[_deleteAllBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted];
[_deleteAllBtn addTarget:self action:@selector(doAllDelete:) forControlEvents:UIControlEventTouchUpInside];
_deleteAllBtn.hidden = YES;
}
return _deleteAllBtn;
}
#pragma mark - load data from coredata
- (void)loadDataList {
NSArray *objs = [searchStrDB getAllSearchStrByDateAscending:NO];
self.dataList = [objs mutableCopy];
// self.dataList = [[NSMutableArray alloc]init];
contentHeight = 0;
if (self.dataList.count > 0) {
_deleteAllBtn.hidden = NO;
contentHeight = (self.dataList.count)*49;
[_tableView reloadData];
}
// if (objs != nil && objs.count > 0) {
// NSMutableArray *strList = [[NSMutableArray alloc] init];
// [objs enumerateObjectsUsingBlock:^(SearchStrList* obj, NSUInteger idx, BOOL *stop) {
// [strList addObject:(NSString*) obj.searchStr];
// }];
// self.dataList = strList;
// if (strList.count) {
// _deleteAllBtn.hidden = NO;
// contentHeight = (strList.count)*49;
// }
// [_tableView reloadData];
// }
// bug:4989 点击搜素框下面部位光标未离开、键盘未收起
footerHeight = rect_screen.size.height - headerHeight - contentHeight;
if (footerHeight > 82) {
_footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, footerHeight);
}
}
- (void)setHeaderHeight:(CGFloat)height {
headerHeight = height;
}
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 49;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *searchHistoryCellID = @"searchHistoryCell";
searchHistoryCell *cell = (searchHistoryCell*)[tableView dequeueReusableCellWithIdentifier:searchHistoryCellID];
if (nil == cell) {
cell = [[searchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:searchHistoryCellID];
}
NSString *searchStr = _dataList[indexPath.row];
[cell setSearchStr:searchStr];
cell.delegate = self;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
if ([self.delegate respondsToSelector:@selector(searchFromHistoryStr:)]) {
NSString *searchStr = _dataList[indexPath.row];
[self.delegate searchFromHistoryStr:searchStr];
}
}
#pragma mark - keyboard
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self closeKeyBoard];
}
- (void)closeKeyBoard {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
#pragma mark - delete event
-(void)doAllDelete:(id)sender{
[self closeKeyBoard];
[searchStrDB deleteAllSearchStr];
NSMutableArray *indexs = [NSMutableArray array];
[_dataList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
[indexs addObject:indexPath];
}];
_dataList = [NSMutableArray array];
contentHeight = 0;
footerHeight = rect_screen.size.height-headerHeight;
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:indexs withRowAnimation:UITableViewRowAnimationAutomatic];
_deleteAllBtn.hidden = YES;
_footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, footerHeight);
[_tableView endUpdates];
}
- (void)deleteStrAtIndex:(NSInteger)index {
[self closeKeyBoard];
NSString* deleteStr = [_dataList objectAtIndex:index];
[searchStrDB deleteSearchStr:deleteStr];
[_dataList removeObjectAtIndex:index];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
contentHeight -= 49;
footerHeight = rect_screen.size.height - headerHeight - contentHeight;
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
if (!_dataList.count) {
_deleteAllBtn.hidden = YES;
}
if (footerHeight > 82) {
_footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, footerHeight);
}
[self.tableView endUpdates];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|