项目介绍:
近期闲来无事,就仿照电商的APP 做了一个分类搜索,包括历史记录,清理历史记录,用数据库封装,给那些需要的人提供点参考!
UIViewController中主要代码:
[Objective-C] 查看源文件 复制代码
- (void)prepareData { /** * 测试数据 ,字段暂时 只用一个 titleString,后续可以根据需求 相应加入新的字段 */ NSDictionary *testDict = @{@"section_id":@"1",@"section_title":@"热搜",@"section_content":@[@{@"content_name":@"化妆棉"},@{@"content_name":@"面膜"},@{@"content_name":@"口红"},@{@"content_name":@"眼霜"},@{@"content_name":@"洗面奶"},@{@"content_name":@"防晒霜"},@{@"content_name":@"补水"},@{@"content_name":@"香水"},@{@"content_name":@"眉笔"}]}; NSMutableArray *testArray = [@[] mutableCopy]; [testArray addObject:testDict]; /*** 去数据查看 是否有数据*/ NSDictionary *parmDict = @{@"category":@"1"}; NSDictionary *dbDictionary = [CXDBHandle statusesWithParams:parmDict]; if (dbDictionary.count) { [testArray addObject:dbDictionary]; [self.searchArray addObjectsFromArray:dbDictionary[@"section_content"]]; } for (NSDictionary *sectionDict in testArray) { CXSearchSectionModel *model = [[CXSearchSectionModel alloc]initWithDictionary:sectionDict]; [self.sectionArray addObject:model]; } } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.cxSearchTextField becomeFirstResponder]; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { CXSearchSectionModel *sectionModel = self.sectionArray[section]; return sectionModel.section_contentArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CXSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cxSearchCollectionViewCell forIndexPath:indexPath]; CXSearchSectionModel *sectionModel = self.sectionArray[indexPath.section]; CXSearchModel *contentModel = sectionModel.section_contentArray[indexPath.row]; [cell.contentButton setTitle:contentModel.content_name forState:UIControlStateNormal]; cell.selectDelegate = self; return cell; } #pragma mark - UICollectionViewDelegate - (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.sectionArray.count; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; if ([kind isEqualToString: UICollectionElementKindSectionHeader]){ SelectCollectionReusableView* view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIden forIndexPath:indexPath]; view.delectDelegate = self; CXSearchSectionModel *sectionModel = self.sectionArray[indexPath.section]; [view setText:sectionModel.section_title]; /*** 此处完全 也可以自定义自己想要的模型对应放入*/ if(indexPath.section == 0) { [view setImage:@"cxCool"]; view.delectButton.hidden = YES; }else{ [view setImage:@"cxSearch"]; view.delectButton.hidden = NO; } reusableview = view; } return reusableview; } - (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CXSearchSectionModel *sectionModel = self.sectionArray[indexPath.section]; if (sectionModel.section_contentArray.count > 0) { CXSearchModel *contentModel = sectionModel.section_contentArray[indexPath.row]; return [CXSearchCollectionViewCell getSizeWithText:contentModel.content_name]; } return CGSizeMake(80, 24); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; }
使用的第三方库有:FMDB
DEMO直接下载:
搜索页面的封装.zip
(269.79 KB, 下载次数: 2220)
2016-7-29 09:30 上传
点击文件名下载附件
近期闲来无事,就仿照电商的APP 做了一个分类搜索,包括历史记录,清理历史记录,用数据库封装,给那些需要 …