//
|
// searchViewController.m
|
// BuWanVideo2.0
|
//
|
// Created by weikou on 16/8/17.
|
// Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "searchViewController.h"
|
#import "YTHSearchTextField.h"
|
|
#import "searchDetailViewController.h"
|
#import "SearchCollectionViewCell.h"
|
#import "SearchCollectionReusableView.h"
|
#import "searchTableViewCell.h"
|
|
//广点通原生广告的头文件
|
#import "ADCollectionReusableView.h"
|
//#import "GDTNativeAd.h"
|
#import "GDTNativeExpressAd.h"
|
#import "GDTNativeExpressAdView.h"
|
#import "FaxianCReusableView.h"
|
|
@interface searchViewController ()<YTHSearchTextFieldDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,GDTNativeExpressAdDelegete>{
|
//广点通原生广告
|
// GDTNativeAd *_nativeAd; //原生广告实例
|
// NSMutableArray *nativeArray;//存储请求下来的原生广告信息
|
}
|
@property (nonatomic , strong) UICollectionView *collectionView;
|
|
@property (nonatomic , strong) YTHSearchTextField *searchField;//搜索框对象
|
@property (nonatomic , strong) NSString *searchString;//搜索框上面的搜索字符串
|
|
@property (weak, nonatomic) UITableView *suggestTableview;//模糊搜索弹出的表格
|
@property (nonatomic , strong) NSMutableArray *suggestSearch;//搜索建议
|
|
@property (nonatomic , strong) NSMutableArray *dataHot;//热门搜索
|
@property (nonatomic , strong) NSMutableArray *dataOld;//历史搜索
|
|
@property (nonatomic , strong) NSMutableArray *historySearchArray;//存放历史搜索记录的数组
|
|
@property (nonatomic, strong) NSArray *expressAdViews;
|
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
|
@end
|
|
@implementation searchViewController
|
|
- (void)viewDidLoad {
|
[super viewDidLoad];
|
//定制导航栏
|
[self setNavgtionView];
|
//添加搜索建议的tableview
|
[self addsuggestTableview];
|
//添加搜索视图
|
[self addSearchCollectionView];
|
//请求数据
|
[self getData];
|
}
|
|
- (void)viewWillAppear:(BOOL)animated
|
{
|
[super viewWillAppear:animated];
|
|
}
|
|
- (void)viewWillDisappear:(BOOL)animated
|
{
|
[super viewWillDisappear:animated];
|
}
|
|
|
/**
|
* 设置导航栏
|
*/
|
- (void)setNavgtionView{
|
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW-115, 30)];
|
YTHSearchTextField *searchField = [[YTHSearchTextField alloc] initWithFrame:CGRectMake(0, 0, titleView.frame.size.width, titleView.frame.size.height)];
|
searchField.layer.cornerRadius =searchField.frame.size.height / 2;
|
searchField.layer.masksToBounds = YES;
|
searchField.delegate = self;
|
searchField.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.2];
|
self.searchField = searchField;
|
[titleView addSubview:searchField];
|
self.navigationItem.titleView = titleView;
|
[searchField.Field resignFirstResponder];
|
if ([YTHsharedManger startManger].searchTitle) {
|
searchField.Field.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[YTHsharedManger startManger].searchTitle attributes:@{NSForegroundColorAttributeName:[UIColor colorWithWhite:0.7 alpha:0.9]}];
|
}
|
//定制返回按钮
|
UIButton *backBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
|
[backBtn setTitle:@"返回" forState:UIControlStateNormal];
|
backBtn.titleLabel.font=[UIFont systemFontOfSize:14];
|
[backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
[backBtn addTarget:self action:@selector(backToMainVC:) forControlEvents:UIControlEventTouchUpInside];
|
UIBarButtonItem *backItem=[[UIBarButtonItem alloc] initWithCustomView:backBtn];
|
|
//添加返回按钮到导航栏
|
self.navigationItem.leftBarButtonItem=backItem;
|
|
//定制搜索按钮
|
UIButton *searchBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
|
[searchBtn setImage:[UIImage imageNamed:@"搜索按钮"] forState:UIControlStateNormal];
|
[searchBtn addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
|
UIBarButtonItem *searchItem=[[UIBarButtonItem alloc] initWithCustomView:searchBtn];
|
|
//添加搜索按钮
|
self.navigationItem.rightBarButtonItem=searchItem;
|
|
//背景颜色
|
self.view.backgroundColor=kGlobalBackgroundColor;
|
}
|
|
|
|
/**
|
* 添加搜索建议的tableView
|
*/
|
-(void)addsuggestTableview{
|
UITableView *suggestTableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
|
[suggestTableview setBackgroundColor:[UIColor whiteColor]];
|
suggestTableview.delegate = self;
|
suggestTableview.dataSource = self;
|
suggestTableview.hidden = YES;
|
_suggestTableview = suggestTableview;
|
[self.view addSubview:suggestTableview];
|
}
|
|
-(void)addSearchCollectionView{
|
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
|
//指定布局方式为垂直
|
flow.scrollDirection = UICollectionViewScrollDirectionVertical;
|
flow.minimumLineSpacing = 5;//最小行间距(当垂直布局时是行间距,当水平布局时可以理解为列间距)
|
flow.minimumInteritemSpacing = 10;//两个单元格之间的最小间距
|
CGFloat height = 64;
|
if (KIsiPhoneX) {
|
height = 84;
|
}
|
//创建CollectionView并指定布局对象
|
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH - height) collectionViewLayout:flow];
|
_collectionView.backgroundColor = kGlobalBackgroundColor;
|
_collectionView.dataSource = self;
|
_collectionView.delegate = self;
|
_collectionView.delaysContentTouches=NO;
|
[self.view addSubview:_collectionView];
|
|
//注册cell
|
[_collectionView registerNib:[UINib nibWithNibName:@"SearchCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SearchCollectionViewCellID"];
|
[self.collectionView registerNib:[UINib nibWithNibName:@"SearchCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchCollectionReusableViewID"];
|
//设置尾部芒果广告的foot
|
[self.collectionView registerNib:[UINib nibWithNibName:@"ADCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ADCollectionReusableView"];
|
[self.collectionView registerNib:[UINib nibWithNibName:@"FaxianCReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView"];
|
|
}
|
|
|
/**
|
* 返回到主视图
|
*
|
* @param sender 返回按钮
|
*/
|
-(void)backToMainVC:(UIButton *)sender{
|
[self.navigationController popViewControllerAnimated:YES];
|
}
|
|
|
|
/**
|
* 点击搜索按钮
|
*
|
* @param sender 搜索按钮
|
*/
|
-(void)search:(UIButton *)sender{
|
if (_searchField.Field.text.length>0) {
|
[self searchCotol:_searchField.Field.text];
|
}else if(_searchField.Field.placeholder.length > 0){
|
[self searchCotol:_searchField.Field.placeholder];
|
}
|
}
|
|
|
-(void)clickADs{
|
// GDTNativeAdData * info =nativeArray[0];
|
// [_nativeAd clickAd:info];
|
}
|
|
|
/**
|
* 搜索建议
|
*
|
* @param text 用户搜索输入的文字
|
*/
|
- (void)getSuggestSearchWithKey:(NSString *)text{
|
_suggestSearch = nil;
|
[[YTHNetInterface startInterface] getSuggestSearchWithUid:[YTHsharedManger startManger].Uid withKey:text withSystem:@"1" withPage:@"1" withblock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
if (!_suggestSearch) {
|
_suggestSearch = [[NSMutableArray alloc]initWithCapacity:0];
|
}
|
_suggestSearch = [[result objectForKey:@"Data"] objectForKey:@"data"];
|
for (int i =0; i <_suggestSearch.count; i++){
|
NSLog(@"%@",[_suggestSearch objectAtIndex:i]);
|
}
|
[_suggestTableview reloadData];
|
}else{
|
//当前网络不稳定
|
}
|
}];
|
}
|
|
/**
|
* 点击搜索执行方法
|
*
|
* @param text 输入框的内容
|
*/
|
- (void)searchCotol:(NSString *)text {
|
[_searchField.Field resignFirstResponder];
|
searchDetailViewController *sresult = [[searchDetailViewController alloc] init];
|
if (_searchField.Field.text.length <1) {
|
sresult.searchString = _searchField.Field.placeholder;
|
[self writeAndReadHistorySearchToFile:_searchField.Field.placeholder];
|
|
}else{
|
sresult.searchString = text;
|
[self writeAndReadHistorySearchToFile:text];
|
}
|
[self getOldSearch];
|
[self.navigationController pushViewController:sresult animated:YES];
|
}
|
|
/**
|
* 将历史搜索写入文件
|
*
|
* @param historySearch 输入框的内容
|
*/
|
-(NSArray *)writeAndReadHistorySearchToFile:(NSString *)historySearch{
|
if(!_historySearchArray){
|
_historySearchArray = [NSMutableArray arrayWithCapacity:0];
|
}
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
if (![fileManager fileExistsAtPath:HISTORYSEARCHFILE]){
|
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_historySearchArray];
|
[fileManager createFileAtPath:HISTORYSEARCHFILE contents:data attributes:nil];
|
if (historySearch) {
|
[_historySearchArray addObject:historySearch];
|
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_historySearchArray];
|
[data writeToFile:HISTORYSEARCHFILE atomically:YES];
|
}
|
return _historySearchArray;
|
}
|
else{
|
_historySearchArray=[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:HISTORYSEARCHFILE]];
|
if (historySearch){
|
if (_historySearchArray.count >=10){
|
[_historySearchArray removeObjectAtIndex:0];
|
}
|
for (int i=0; i<_historySearchArray.count; i++){
|
if ([[_historySearchArray objectAtIndex:i] isEqual:historySearch]){
|
return _historySearchArray;
|
}
|
}
|
[_historySearchArray addObject:historySearch];
|
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_historySearchArray];
|
[data writeToFile:HISTORYSEARCHFILE atomically:YES];
|
}
|
}
|
return _historySearchArray;
|
}
|
|
/**
|
* 请求数据
|
*/
|
- (void)getData{
|
[self getHotSearch];
|
[self getOldSearch];
|
[self loadAd];
|
}
|
|
/**
|
* 热门搜索
|
*/
|
- (void)getHotSearch{
|
[[YTHNetInterface startInterface] getHotSerachWithUid:[YTHsharedManger startManger].Uid withSystem:@"1" withblock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
NSDictionary *dic = (NSDictionary *)result;
|
if (!_dataHot) {
|
_dataHot = [[NSMutableArray alloc] initWithCapacity:0];
|
}
|
[_dataHot removeAllObjects];
|
NSArray *ar = [[dic objectForKey:@"Data"] objectForKey:@"data"];
|
for (int i =0; i<ar.count; i++) {
|
[_dataHot addObject:[ar objectAtIndex:i]];
|
}
|
// self.searchField
|
//刷新热门搜索
|
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
|
}else{
|
if ([error compare:@"似乎已断开与互联网的连接。"] == 0) {
|
[self autoDisappearAlertTime:1 msg:@"网络不可用,请检查网络"];
|
}
|
//显示网络连接失败
|
}
|
}];
|
}
|
|
/**
|
* 历史搜索 读取本地搜素记录
|
*/
|
- (void)getOldSearch{
|
_dataOld = (NSMutableArray *)[self writeAndReadHistorySearchToFile:nil];
|
//刷新本地记录
|
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
|
}
|
|
/**
|
* 加载广点通原生广告
|
*/
|
-(void)loadAd{
|
// _nativeAd = [[GDTNativeAd alloc] initWithAppkey:GDTADkey placementId:GDTYSADkey3];
|
// _nativeAd.controller = self;
|
// _nativeAd.delegate = self;
|
// [_nativeAd loadAd:1];
|
// 支持视频广告的 PlacementId 会混出视频与图片广告
|
self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithPlacementId:GDTYSADkey3 adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
|
self.nativeExpressAd.delegate = self;
|
// 配置视频播放属性
|
self.nativeExpressAd.videoAutoPlayOnWWAN = YES;
|
self.nativeExpressAd.videoMuted = NO;
|
[self.nativeExpressAd loadAd:2];
|
}
|
- (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd
|
*)nativeExpressAd views:(NSArray<__kindof
|
GDTNativeExpressAdView *> *)views
|
{
|
self.expressAdViews = [NSArray arrayWithArray:views];
|
if (self.expressAdViews.count) {
|
[self.expressAdViews enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
|
expressView.controller = self;
|
[expressView render];
|
}];
|
}
|
// 广告位 render 后刷新 tableView
|
[_collectionView reloadData];
|
}
|
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error{
|
NSLog(@"%@",error);
|
}
|
|
/**
|
* 清除历史记录
|
*
|
* @param sender 清除的按钮
|
*/
|
-(void)ClearHistorys:(UIButton *)sender{
|
[_dataOld removeAllObjects];
|
[self deleteFile:HISTORYSEARCHFILE];
|
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
|
}
|
|
|
/**
|
* 删除沙盒里的文件
|
*
|
* @param filePath 要删除的文件
|
*/
|
-(void)deleteFile:(NSString *)filePath {
|
NSFileManager* fileManager=[NSFileManager defaultManager];
|
BOOL blHave=[fileManager fileExistsAtPath:filePath];
|
if (!blHave) {
|
NSLog(@"no have");
|
return ;
|
}else {
|
NSLog(@"have");
|
BOOL blDele= [fileManager removeItemAtPath:filePath error:nil];
|
if (blDele) {
|
NSLog(@"dele success");
|
}else {
|
NSLog(@"dele fail");
|
}
|
}
|
}
|
|
/***************(以下)协议方法***************/
|
#pragma mark -GDTNativeAdDelegate
|
/**
|
* 原生广告加载广告数据成功回调,返回为GDTNativeAdData对象的数组
|
*/
|
//-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
|
// if (!nativeArray) {
|
// nativeArray =[[NSMutableArray alloc]initWithArray:nativeAdDataArray];
|
// }else{
|
// [nativeArray addObjectsFromArray:nativeAdDataArray];
|
// }
|
// [_collectionView reloadData];
|
//}
|
|
/**
|
* 原生广告加载广告数据失败回调
|
*/
|
//-(void)nativeAdFailToLoad:(NSError *)error{
|
// NSLog(@"XYR_%@",error);
|
//}
|
|
#pragma mark -YTHSearchTextFieldDelegate
|
- (void)YTHSearchField:(YTHSearchTextField *)searchField SearchValueChanged:(UITextField *)textField{//实时监控
|
if (textField.text.length>0) {
|
_suggestTableview.hidden = NO;
|
_collectionView.hidden=YES;
|
NSLog(@"有数据");
|
[self getSuggestSearchWithKey:textField.text];
|
}else{
|
_suggestTableview.hidden = YES;
|
_collectionView.hidden=NO;
|
NSLog(@"无数据");
|
}
|
}
|
|
- (BOOL)YTHSearchField:(YTHSearchTextField *)searchField textFieldShouldReturn:(UITextField *)textField{
|
NSLog(@"点击了键盘搜索");
|
if (textField.text.length>0) {
|
[self searchCotol:textField.text];
|
}
|
return YES;
|
}
|
|
- (void)YTHSearchField:(YTHSearchTextField *)searchField clearClik:(UIButton *)btn{//点击清除按钮
|
self.suggestTableview.hidden = YES;
|
self.collectionView.hidden=NO;
|
[self getOldSearch];
|
}
|
|
#pragma mark -UITableViewDataSource
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
return _suggestSearch.count;
|
}
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
static NSString *indentfly = @"cell";
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly];
|
if (cell == nil) {
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly];
|
}
|
cell.textLabel.text = [_suggestSearch objectAtIndex:indexPath.row];
|
return cell;
|
}
|
|
#pragma mark -UITableViewDelegate
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
|
return 0.1;
|
}
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
|
return 44;
|
}
|
|
//点击建议搜索进行搜索
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
|
_searchField.Field.text = [_suggestSearch objectAtIndex:indexPath.row];
|
[self searchCotol:_searchField.Field.text];
|
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
|
[tableView reloadData];
|
}
|
|
#pragma mark -UICollectionViewDataSource
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
if (section==0) {
|
if (_dataHot.count<10) {
|
return _dataHot.count;
|
}
|
return 10;
|
}else if(section==1){
|
if(_dataOld.count<10){
|
return _dataOld.count;
|
}else{
|
return 10;
|
}
|
}else{
|
return 0;
|
}
|
}
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
SearchCollectionViewCell *Cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"SearchCollectionViewCellID" forIndexPath:indexPath];
|
if (indexPath.section==0) {//热门搜索的cell
|
Cell.searchCellText.text=_dataHot[indexPath.row];
|
[Cell.backgroundColor setHidden:YES];
|
UIImage *deselectImage;
|
if (indexPath.row%2==0) {//热门搜索单双数的cell背景不同,需要分别设置
|
deselectImage=[UIImage imageNamed:@"热门搜索头"];
|
}else{
|
deselectImage=[UIImage imageNamed:@"热门搜索尾"];
|
}
|
deselectImage=[deselectImage stretchableImageWithLeftCapWidth:floorf(deselectImage.size.width/2) topCapHeight:floorf(deselectImage.size.height/2)];
|
[Cell.searchCellBackImage setImage:deselectImage];
|
}else{
|
//历史搜索的cell
|
Cell.searchCellText.text=_dataOld[_dataOld.count-indexPath.row-1];
|
Cell.searchCellText.backgroundColor=[UIColor whiteColor];
|
[Cell.searchCellBackImage setHidden:YES];
|
}
|
return Cell;
|
}
|
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
|
return 2;
|
}
|
//定制collectionView的head和foot
|
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
|
if (kind == UICollectionElementKindSectionHeader){
|
SearchCollectionReusableView *searchV=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchCollectionReusableViewID" forIndexPath:indexPath];
|
if (indexPath.section==0) {
|
searchV.searchViewTitle.text=@"热门搜索";
|
[searchV.ClearHistory setHidden:YES];
|
}else{
|
searchV.searchViewTitle.text=@"历史搜索";
|
[searchV.ClearHistory addTarget:self action:@selector(ClearHistorys:) forControlEvents:UIControlEventTouchUpInside];
|
}
|
return searchV;
|
}else if (kind == UICollectionElementKindSectionFooter){
|
if (indexPath.section==1) {
|
//返回广告视图
|
ADCollectionReusableView *groupfootSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ADCollectionReusableView" forIndexPath:indexPath];
|
// GDTNativeAdData *info=nativeArray[0];
|
//
|
// groupfootSection.ADTitle.text=[info.properties objectForKey:GDTNativeAdDataKeyTitle];
|
// groupfootSection.ADTitle.backgroundColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
|
// [groupfootSection.ADImage setYthImageWithURL:[info.properties objectForKey:GDTNativeAdDataKeyImgUrl] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
|
// //把button设为透明
|
// groupfootSection.ADClickButton.backgroundColor=[UIColor clearColor];
|
// [groupfootSection.ADClickButton addTarget:self action:@selector(clickADs) forControlEvents:UIControlEventTouchUpInside];
|
// [_nativeAd attachAd:info toView:groupfootSection];
|
GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.expressAdViews[0];
|
// FaxianCReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FaxianCReusableView" forIndexPath:indexPath];
|
// [view addSubview:groupfootSection];
|
[groupfootSection addSubview:adView];
|
return groupfootSection;
|
// return view;
|
|
}
|
}
|
return nil;
|
}
|
#pragma mark -UICollectionViewDelegate
|
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
|
return YES;
|
}
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
|
SearchCollectionViewCell *Cell=(SearchCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
|
self.searchField.Field.text=Cell.searchCellText.text;
|
if (_searchField.Field.text.length>0) {
|
[self searchCotol:_searchField.Field.text];
|
}
|
}
|
#pragma mark -UICollectionViewDelegateFlowLayout
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
|
if (indexPath.section==0) {
|
return CGSizeMake((KScreenW-40)/2, 30);
|
}else if(indexPath.section==1){
|
NSString *IntroductionStr=_dataOld[_dataOld.count-indexPath.row-1];
|
CGSize IntroSize = [IntroductionStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 25) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
|
|
CGSize titleSize=CGSizeMake((IntroSize.width+10)<(KScreenW-40)/2?IntroSize.width+30:(KScreenW-50)/2, 25);
|
return titleSize;
|
}
|
CGSize defaultSize=CGSizeMake(0.0f, 0.0f);
|
return defaultSize;
|
}
|
//协议中的方法,用于返回整个CollectionView上、左、下、右距四边的间距
|
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
|
if(section==0){
|
//上、左、下、右的边距
|
return UIEdgeInsetsMake(0, 15, 10, 15);
|
}else if(section==1){
|
return UIEdgeInsetsMake(10, 15, 10, 15);
|
}else{
|
return UIEdgeInsetsMake(0, 0, 0, 0);
|
}
|
}
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
|
{
|
//宽度随便定,系统会自动取collectionView的宽度
|
//高度为分组头的高度
|
if(section==0){
|
return CGSizeMake(0, 50);
|
}else{
|
return CGSizeMake(0, 30);
|
}
|
}
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
|
if(section==1){
|
if (self.expressAdViews==nil) {
|
return CGSizeMake(0, 0);
|
}else{
|
UIView *view = self.expressAdViews[0];
|
|
return CGSizeMake(0, view.bounds.size.height);
|
}
|
}else{
|
return CGSizeMake(0, 0);
|
}
|
}
|
|
@end
|