searchVC.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. //
  2. // searchVC.m
  3. // YBVideo
  4. //
  5. // Created by 王敏欣 on 2017/2/17.
  6. // Copyright © 2017年 cat. All rights reserved.
  7. //
  8. #import "searchVC.h"
  9. #import "HXSearchBar.h"
  10. #import "fansModel.h"
  11. #import "fans.h"
  12. #import "YBCenterVC.h"
  13. #import "SearchHistoryCell.h"
  14. #import "HMSegmentedControl.h"
  15. #import "NearbyVideoModel.h"
  16. #import "VideoCollectionCell.h"
  17. #import "YBGetVideoObj.h"
  18. #import "YBSearchBarView.h"
  19. #import "LiveRankVC.h"
  20. #import "LiveRankCell.h"
  21. #import "YBCheckLiveObj.h"
  22. @interface searchVC ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,guanzhu,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
  23. {
  24. // HXSearchBar *searchBars;
  25. YBSearchBarView *searchView;
  26. NSInteger _userPaging;
  27. NSInteger _videoPaging;
  28. BOOL isHistory; //是否显示历史
  29. BOOL isUser; //是用户还是视频列表
  30. }
  31. @property(nonatomic,strong)UITableView *historyTableView; //搜索历史
  32. @property(nonatomic,strong)NSMutableArray *historyArray;
  33. @property(nonatomic,strong)UIView *userVideoMix; //搜索结果(用户和视频组合)
  34. @property(nonatomic,strong)HMSegmentedControl *segmentedControl; //segment
  35. @property(nonatomic,strong)UIScrollView *bgScrollView; //底部scrollView(承载用户、视频搜索结果界面)
  36. @property (strong, nonatomic)UITableView *userTableView; //用户搜索
  37. @property(nonatomic,strong)NSArray *userModels;
  38. @property(nonatomic,strong)NSMutableArray *userArray;
  39. @property(nonatomic,strong)UICollectionView *videoCollectinView; //视频搜索
  40. @property(nonatomic,strong)NSArray *videoModels;
  41. @property(nonatomic,strong)NSMutableArray *videoArray;
  42. @property(nonatomic,strong)UIView *tabHeaderView;
  43. @property(nonatomic,strong)UIView *tabFooterView;
  44. @property(nonatomic,strong)NSArray *weekArr;
  45. @end
  46. @implementation searchVC
  47. - (void)viewWillAppear:(BOOL)animated {
  48. [super viewWillAppear:animated];
  49. //[self getUsers];
  50. //[self getVideoLists];
  51. //获取搜索历史
  52. _historyArray = [NSMutableArray arrayWithArray:[common getHistoryArray]];
  53. [self refreshHistory];
  54. }
  55. - (void)viewWillDisappear:(BOOL)animated {
  56. [super viewWillDisappear:animated];
  57. }
  58. - (UIStatusBarStyle)preferredStatusBarStyle {
  59. if (@available(iOS 13.0,*)) {
  60. return UIStatusBarStyleDarkContent;
  61. }
  62. return UIStatusBarStyleDefault;
  63. }
  64. - (void)viewDidLoad {
  65. [super viewDidLoad];
  66. self.naviView.hidden = YES;
  67. self.navigationController.interactivePopGestureRecognizer.delegate = (id) self;
  68. _userPaging = 1;
  69. _videoPaging = 1;
  70. // self.view.backgroundColor = Normal_Color;
  71. self.view.backgroundColor = RGB(250, 250, 250);
  72. self.subNavi.backgroundColor = UIColor.whiteColor;
  73. self.titleL.textColor = UIColor.blackColor;
  74. [self.leftBtn setImage:[UIImage imageNamed:@"pub_back_black"] forState:0];
  75. self.naviLine.hidden = NO;
  76. self.naviLine.backgroundColor = RGB(245, 245, 245);
  77. //历史
  78. self.historyArray = [NSMutableArray array];
  79. isHistory = YES;
  80. //用户搜索
  81. self.userArray = [NSMutableArray array];
  82. self.userModels = [NSArray array];
  83. isUser = YES;
  84. //视频搜索
  85. self.videoModels = [NSArray array];
  86. self.videoArray = [NSMutableArray array];
  87. [self addSearchBar];
  88. [self.view addSubview:self.historyTableView];
  89. //用户、视频组合
  90. [self.view addSubview:self.userVideoMix];
  91. self.weekArr = [NSArray array];
  92. [self getWeekShowLists];
  93. // [self.view addSubview:self.userTableView];
  94. // _userTableView.hidden = YES;
  95. // [self.view addSubview:self.videoCollectinView];
  96. }
  97. -(void)getWeekShowLists{
  98. [YBNetworking postWithUrl:@"Home.getWeekShowLists" Dic:nil Suc:^(int code, id info, NSString *msg) {
  99. _weekArr = info;
  100. [_historyTableView reloadData];
  101. } Fail:^(id fail) {
  102. }];
  103. }
  104. #pragma mark - 刷新历史
  105. -(void)refreshHistory {
  106. // if (_historyArray.count<=0) {
  107. // [PublicView showTextNoData:_historyTableView text1:@"" text2:YZMsg(@"还没有搜索历史~") centerY:0.8];
  108. // }else {
  109. // [PublicView hiddenTextNoData:_historyTableView];
  110. // }
  111. [_historyTableView reloadData];
  112. }
  113. #pragma mark - 用户数据
  114. -(NSArray *)userModels{
  115. NSMutableArray *array = [NSMutableArray array];
  116. for (NSDictionary *dic in self.userArray) {
  117. fansModel *model = [fansModel modelWithDic:dic];
  118. [array addObject:model];
  119. }
  120. _userModels = array;
  121. return _userModels;
  122. }
  123. -(void)getUsers{
  124. NSString *url = [NSString stringWithFormat:@"Home.search&key=%@&uid=%@&token=%@&p=%ld",searchView.searchTF.text,[Config getOwnID],[Config getOwnToken],(long)_userPaging];
  125. if (_searchFrom == SearchFrom_City) {
  126. url = [url stringByAppendingFormat:@"&lng=%@&lat=%@&city=%@",[cityDefault getLocationLng],[cityDefault getLocationLat],_curCity];
  127. }
  128. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  129. YBWeakSelf;
  130. [YBNetworking postWithUrl:url Dic:nil Suc:^(int code, id info, NSString *msg) {
  131. [_userTableView.mj_header endRefreshing];
  132. [_userTableView.mj_footer endRefreshing];
  133. if(code == 0) {
  134. NSArray *infoA = [NSArray arrayWithArray:info];
  135. if (_userPaging == 1) {
  136. [_userArray removeAllObjects];
  137. }
  138. if (infoA.count <=0) {
  139. [_userTableView.mj_footer endRefreshingWithNoMoreData];
  140. }else {
  141. [_userArray addObjectsFromArray:infoA];
  142. }
  143. if (_userArray.count <= 0) {
  144. [PublicView showTextNoData:_userTableView text1:YZMsg(@"对不起") text2:YZMsg(@"没有搜索到相关内容") centerY:0.8];
  145. }else{
  146. [PublicView hiddenTextNoData:_userTableView];
  147. }
  148. [_userTableView reloadData];
  149. }else {
  150. [PublicView showTextNoData:weakSelf.userTableView text1:YZMsg(@"对不起") text2:YZMsg(@"没有搜索到相关内容") centerY:0.8];
  151. }
  152. } Fail:^(id fail) {
  153. [_userTableView.mj_header endRefreshing];
  154. [_userTableView.mj_footer endRefreshing];
  155. [_userArray removeAllObjects];
  156. [_userTableView reloadData];
  157. [PublicView showTextNoData:weakSelf.userTableView text1:YZMsg(@"对不起") text2:YZMsg(@"没有搜索到相关内容") centerY:0.8];
  158. }];
  159. }
  160. #pragma mark - 视频数据
  161. -(NSArray *)videoModels {
  162. NSMutableArray *m_array = [NSMutableArray array];
  163. for (NSDictionary *dic in _videoArray) {
  164. NearbyVideoModel *model = [NearbyVideoModel modelWithDic:dic];
  165. [m_array addObject:model];
  166. }
  167. _videoModels = m_array;
  168. return _videoModels;
  169. }
  170. -(void)getVideoLists {
  171. NSString *url = [NSString stringWithFormat:@"Home.videoSearch&uid=%@&key=%@&token=%@&p=%ld",[Config getOwnID],searchView.searchTF.text,[Config getOwnToken],(long)_videoPaging];
  172. if (_searchFrom == SearchFrom_City) {
  173. url = [url stringByAppendingFormat:@"&lng=%@&lat=%@&city=%@",[cityDefault getLocationLng],[cityDefault getLocationLat],_curCity];
  174. }
  175. url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  176. [YBNetworking postWithUrl:url Dic:nil Suc:^(int code, id info, NSString *msg) {
  177. [_videoCollectinView.mj_header endRefreshing];
  178. [_videoCollectinView.mj_footer endRefreshing];
  179. if (code == 0) {
  180. NSArray *infoA = [NSArray arrayWithArray:info];
  181. if (_videoPaging == 1) {
  182. [_videoArray removeAllObjects];
  183. }
  184. if (infoA.count <=0) {
  185. [_videoCollectinView.mj_footer endRefreshingWithNoMoreData];
  186. }else{
  187. [_videoArray addObjectsFromArray:infoA];
  188. }
  189. if (_videoArray.count<=0) {
  190. [PublicView showTextNoData:_videoCollectinView text1:YZMsg(@"对不起") text2:YZMsg(@"没有搜索到相关内容") centerY:0.8];
  191. }else{
  192. [PublicView hiddenTextNoData:_videoCollectinView];
  193. }
  194. [_videoCollectinView reloadData];
  195. }else {
  196. [MBProgressHUD showPop:msg];
  197. }
  198. } Fail:^(id fail) {
  199. [_videoCollectinView.mj_header endRefreshing];
  200. [_videoCollectinView.mj_footer endRefreshing];
  201. [_videoArray removeAllObjects];
  202. [_videoCollectinView reloadData];
  203. [PublicView showTextNoData:_videoCollectinView text1:@"" text2:YZMsg(@"网络错误") centerY:0.8];
  204. }];
  205. }
  206. #pragma mark - 清空历史
  207. -(void)clearAllHistory {
  208. [_historyArray removeAllObjects];
  209. [common saveHistory:[NSArray arrayWithArray:_historyArray]];
  210. [self refreshHistory];
  211. }
  212. #pragma mark - 用户列表关注
  213. -(void)doGuanzhu:(NSString *)st{
  214. [self getUsers];
  215. }
  216. #pragma mark - scrollView 代理
  217. -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  218. [self.view endEditing:YES];
  219. }
  220. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  221. if (scrollView == _bgScrollView) {
  222. CGFloat pageWidth = scrollView.frame.size.width;
  223. NSInteger page = scrollView.contentOffset.x / pageWidth;
  224. [_segmentedControl setSelectedSegmentIndex:page animated:YES];
  225. }
  226. }
  227. //查看更多
  228. -(void)moreLiveRank{
  229. LiveRankVC *rankVc = [[LiveRankVC alloc]init];
  230. [[YBBaseAppDelegate sharedAppDelegate]pushViewController:rankVc animated:YES];
  231. }
  232. #pragma mark - tableView 代理
  233. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  234. if (isHistory) {
  235. NSLog(@"hhhh----hhhh111");
  236. if (section == 0) {
  237. return 50;
  238. }else{
  239. return 0;
  240. }
  241. }
  242. NSLog(@"hhhh----hhhh222");
  243. return 0;
  244. }
  245. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  246. if (isHistory) {
  247. NSLog(@"hhhh----hhhh111");
  248. if (section == 0) {
  249. return 50;
  250. }else{
  251. return 0;
  252. }
  253. }
  254. NSLog(@"hhhh----hhhh222");
  255. return 0;
  256. }
  257. -(UIView *)tabHeaderView {
  258. if (!_tabHeaderView) {
  259. _tabHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 50)];
  260. _tabHeaderView.backgroundColor = UIColor.whiteColor;//CellRow_Cor;
  261. UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 180, 50)];
  262. title.font = SYS_Font(15);
  263. title.text = YZMsg(@"搜索历史");
  264. title.textColor = GrayText;
  265. [_tabHeaderView addSubview:title];
  266. UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(15, 49.5, _window_width-30, 0.5)];
  267. line.backgroundColor = RGB_COLOR(@"#ffffff", 0.06);
  268. [_tabHeaderView addSubview:line];
  269. UIButton *clearHistory = [UIButton buttonWithType:UIButtonTypeCustom];
  270. clearHistory.frame = CGRectMake(_window_width-75, 0, 60, 50);
  271. [clearHistory setTitle:YZMsg(@"清空") forState:0];
  272. clearHistory.titleLabel.font = SYS_Font(15);
  273. [clearHistory setTitleColor:GrayText forState:0];
  274. clearHistory.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  275. [clearHistory addTarget:self action:@selector(clearAllHistory) forControlEvents:UIControlEventTouchUpInside];
  276. [_tabHeaderView addSubview:clearHistory];
  277. }
  278. return _tabHeaderView;
  279. }
  280. -(UIView *)tabFooterView {
  281. if (!_tabFooterView) {
  282. _tabFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 50)];
  283. _tabFooterView.backgroundColor = UIColor.whiteColor;// RGB_COLOR(@"#09051E", 1);
  284. UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 180, 50)];
  285. title.font = SYS_Font(14);
  286. title.text = YZMsg(@"直播榜单");
  287. title.textColor = GrayText;
  288. [_tabFooterView addSubview:title];
  289. UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(15, 49.5, _window_width-30, 0.5)];
  290. line.backgroundColor = RGB_COLOR(@"#ffffff", 0.06);
  291. [_tabFooterView addSubview:line];
  292. UIButton *clearHistory = [UIButton buttonWithType:UIButtonTypeCustom];
  293. clearHistory.frame = CGRectMake(_window_width-95, 0, 80, 50);
  294. [clearHistory setTitle:YZMsg(@"查看更多") forState:0];
  295. clearHistory.titleLabel.font = SYS_Font(14);
  296. [clearHistory setTitleColor:Pink_Cor forState:0];
  297. [clearHistory setImage:[UIImage imageNamed:@"shop_right"] forState:0];
  298. [clearHistory setTitleEdgeInsets:UIEdgeInsetsMake(0, - clearHistory.imageView.image.size.width, 0, clearHistory.imageView.image.size.width)];
  299. [clearHistory setImageEdgeInsets:UIEdgeInsetsMake(0, clearHistory.titleLabel.bounds.size.width, 0, -clearHistory.titleLabel.bounds.size.width)];
  300. clearHistory.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  301. [clearHistory addTarget:self action:@selector(moreLiveRank) forControlEvents:UIControlEventTouchUpInside];
  302. [_tabFooterView addSubview:clearHistory];
  303. }
  304. return _tabFooterView;
  305. }
  306. -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  307. if (isHistory) {
  308. NSLog(@"hhhh----vvvvv111==%@",self.tabHeaderView);
  309. if (section == 0) {
  310. return self.tabHeaderView;
  311. }else{
  312. return nil;
  313. }
  314. }
  315. NSLog(@"hhhh----vvvvv222");
  316. return nil;
  317. }
  318. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  319. {
  320. if (isHistory) {
  321. if (section == 0) {
  322. return self.tabFooterView;
  323. }else{
  324. return nil;
  325. }
  326. }
  327. return nil;
  328. }
  329. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  330. {
  331. if (isHistory) {
  332. return 2;
  333. }else{
  334. return 1;
  335. }
  336. }
  337. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  338. if (isHistory == NO) {
  339. return self.userModels.count;
  340. }else{
  341. //搜索历史
  342. if (section == 0) {
  343. return _historyArray.count;
  344. }else{
  345. return _weekArr.count;
  346. }
  347. }
  348. }
  349. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  350. if (isHistory == NO) {
  351. return 90;
  352. }
  353. if (indexPath.section == 0) {
  354. return 50;
  355. }else{
  356. return 50;
  357. }
  358. }
  359. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  360. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  361. if (isHistory == NO) {
  362. //用户
  363. fans *cell = [fans cellWithTableView:tableView];
  364. cell.backgroundColor = UIColor.whiteColor;
  365. fansModel *model = _userModels[indexPath.row];
  366. cell.model = model;
  367. cell.guanzhuDelegate = self;
  368. if (cell.model.uid ==[Config getOwnID] || [cell.model.uid isEqualToString:[Config getOwnID]]) {
  369. cell.guanzhubtn.hidden = YES;
  370. }
  371. cell.nameL.textColor = UIColor.blackColor;
  372. cell.lineLb.backgroundColor = RGB(245, 245, 245);
  373. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  374. return cell;
  375. }else {
  376. if (indexPath.section == 0) {
  377. //搜索历史
  378. SearchHistoryCell *cell = [SearchHistoryCell cellWithTab:tableView andIndexPath:indexPath];
  379. cell.delEvent = ^(NSString *type) {
  380. if (_historyArray.count >indexPath.row) {
  381. [_historyArray removeObjectAtIndex:indexPath.row];
  382. [common saveHistory:[NSArray arrayWithArray:_historyArray]];
  383. [self refreshHistory];
  384. }
  385. };
  386. cell.contentL.text = _historyArray[indexPath.row];
  387. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  388. return cell;
  389. }else{
  390. LiveRankCell *cell = [LiveRankCell cellWithTab:tableView andIndexPath:indexPath];
  391. if (indexPath.row < 3) {
  392. cell.numImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"live_rank%ld",indexPath.row+1]];
  393. cell.numLb.hidden = YES;
  394. }else{
  395. cell.numLb.hidden = NO;
  396. cell.numLb.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
  397. }
  398. cell.rankData = _weekArr[indexPath.row];
  399. return cell;
  400. }
  401. }
  402. }
  403. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  404. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  405. [self.view endEditing:YES];
  406. if (isHistory == NO) {
  407. fansModel *model = _userModels[indexPath.row];
  408. YBCenterVC *center = [[YBCenterVC alloc]init];
  409. center.otherUid = model.uid;
  410. center.isPush = YES;
  411. //[self.navigationController pushViewController:center animated:YES];
  412. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:center animated:YES];
  413. }else {
  414. if (indexPath.section == 0) {
  415. searchView.searchTF.text = _historyArray[indexPath.row];
  416. [self searchBarSearchButtonClicked];
  417. }else{
  418. NSDictionary *rankDic = _weekArr[indexPath.row];
  419. if ([minstr([rankDic valueForKey:@"islive"]) isEqual:@"1"]) {
  420. [YBCheckLiveObj checkLiveManeger].liveUid = minstr([rankDic valueForKey:@"uid"]);
  421. [YBCheckLiveObj checkLiveManeger].liveStream = minstr([rankDic valueForKey:@"stream"]);
  422. [YBCheckLiveObj checkLiveManeger].currentIndex = 0;
  423. [YBCheckLiveObj checkLiveManeger].listArray = nil;
  424. [[YBCheckLiveObj checkLiveManeger] checkLiving];
  425. }else{
  426. YBCenterVC *center = [[YBCenterVC alloc]init];
  427. center.otherUid = minstr([rankDic valueForKey:@"uid"]);
  428. center.isPush = YES;
  429. //[self.navigationController pushViewController:center animated:YES];
  430. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:center animated:YES];
  431. }
  432. }
  433. }
  434. }
  435. #pragma mark - CollectionView 代理
  436. /*
  437. * minimumLineSpacing、minimumInteritemSpacing去设置
  438. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  439. return CGSizeMake(0,0);
  440. }
  441. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  442. {
  443. return UIEdgeInsetsMake(0,0,0,0);
  444. }
  445. */
  446. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  447. return 2;
  448. }
  449. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  450. return 1;
  451. }
  452. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  453. return self.videoModels.count;
  454. }
  455. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  456. VideoCollectionCell *cell = (VideoCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCollectionCell" forIndexPath:indexPath];
  457. NearbyVideoModel *model = _videoModels[indexPath.row];
  458. cell.model = model;
  459. cell.backgroundColor = CellRow_Cor;
  460. return cell;
  461. }
  462. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  463. NSString *vidoeID = minstr([_videoArray[indexPath.row] valueForKey:@"id"]);
  464. [YBGetVideoObj lookManeger].fromWhere = @"searchVC";
  465. [YBGetVideoObj lookManeger].videoID = vidoeID;
  466. [YBGetVideoObj lookManeger].playIndex = (int)indexPath.row;
  467. [YBGetVideoObj lookManeger].videoList = [_videoArray mutableCopy];
  468. [YBGetVideoObj lookManeger].paging = (int)_videoPaging;
  469. NSString *baseUrl = [NSString stringWithFormat:@"Home.videoSearch&key=%@",searchView.searchTF.text];
  470. [YBGetVideoObj lookManeger].baseUrl = baseUrl;
  471. [[YBGetVideoObj lookManeger]goLookVC];
  472. }
  473. #pragma mark - set/get
  474. //添加搜索条
  475. - (void)addSearchBar {
  476. UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, statusbarHeight+70)];
  477. bg.backgroundColor = UIColor.whiteColor;
  478. [self.view addSubview:bg];
  479. YBWeakSelf;
  480. searchView = [[YBSearchBarView alloc]initWithFrame:CGRectMake(10,10+statusbarHeight, self.view.frame.size.width -20,60)];
  481. searchView.searchEvent = ^(RKSearchType searchType) {
  482. [weakSelf searchEevnt:searchType];
  483. };
  484. [bg addSubview:searchView];
  485. [searchView.searchTF becomeFirstResponder];
  486. }
  487. #pragma mark - 搜索事件 s
  488. -(void)searchEevnt:(RKSearchType)searchType {
  489. if (searchType == RKSearch_Search) {
  490. //搜索
  491. [self searchBarSearchButtonClicked];
  492. }
  493. if (searchType == RKSearch_Cancle) {
  494. //取消
  495. searchView.searchTF.text = @"";
  496. [searchView.searchTF resignFirstResponder];
  497. [self.view endEditing:YES];
  498. [self.navigationController popViewControllerAnimated:YES];
  499. [self dismissViewControllerAnimated:YES completion:nil];
  500. }
  501. if (searchType == RKSearch_ValueChange) {
  502. //输入框改变
  503. NSString *allText = [searchView.searchTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  504. if (allText.length > 0) {
  505. isHistory = NO;
  506. _historyTableView.hidden = YES;
  507. _userVideoMix.hidden = NO;
  508. [_userArray removeAllObjects];
  509. [_userTableView reloadData];
  510. _userPaging = 1;
  511. _videoPaging = 1;
  512. [self getUsers];
  513. [self getVideoLists];
  514. [self addHistory];
  515. }else{
  516. isHistory = YES;
  517. _userVideoMix.hidden = YES;
  518. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  519. _historyTableView.hidden = NO;
  520. [self refreshHistory];
  521. });
  522. }
  523. }
  524. if (searchType == RKSearch_BeginEditing) {
  525. //开始编辑
  526. if (searchView.searchTF.text.length>0) {
  527. isHistory = NO;
  528. _historyTableView.hidden = YES;
  529. _userVideoMix.hidden = NO;
  530. }else{
  531. isHistory = YES;
  532. _historyTableView.hidden = NO;
  533. _userVideoMix.hidden = YES;
  534. }
  535. }
  536. }
  537. //搜索
  538. - (void)searchBarSearchButtonClicked{
  539. NSString *allText = [searchView.searchTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  540. if (allText.length<1) {
  541. [MBProgressHUD showPop:YZMsg(@"请输入内容")];
  542. return;
  543. }
  544. isHistory = NO;
  545. _historyTableView.hidden = YES;
  546. _userVideoMix.hidden = NO;
  547. [self addHistory];
  548. [_userArray removeAllObjects];
  549. [_userTableView reloadData];
  550. _userPaging = 1;
  551. _videoPaging = 1;
  552. [self getUsers];
  553. [self getVideoLists];
  554. [searchView.searchTF resignFirstResponder];
  555. }
  556. -(void)addHistory {
  557. if (searchView.searchTF.text.length>0) {
  558. for (NSString *str in _historyArray) {
  559. if ([str isEqual:searchView.searchTF.text]) {
  560. [_historyArray removeObject:str];
  561. break;
  562. }
  563. }
  564. //加入搜索历史
  565. [_historyArray insertObject:searchView.searchTF.text atIndex:0];
  566. //最多六条
  567. if (_historyArray.count>6) {
  568. [_historyArray removeLastObject];
  569. }
  570. [common saveHistory:[NSArray arrayWithArray:_historyArray]];
  571. }
  572. }
  573. #pragma mark - 搜索事件 end
  574. -(UITableView *)historyTableView {
  575. if (!_historyTableView) {
  576. //5个像素间隔
  577. _historyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 75+statusbarHeight, _window_width, _window_height-75-statusbarHeight-ShowDiff) style:UITableViewStylePlain];
  578. _historyTableView.delegate = self;
  579. _historyTableView.dataSource = self;
  580. _historyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  581. _historyTableView.backgroundColor =UIColor.whiteColor;// RGB_COLOR(@"#09051E", 1);//Normal_Color;
  582. }
  583. return _historyTableView;
  584. }
  585. -(UIView *)userVideoMix {
  586. if (!_userVideoMix) {
  587. CGFloat mixHeight = _window_height-75-statusbarHeight-ShowDiff;
  588. CGFloat segHeight = 40;
  589. CGFloat scrollHeight = mixHeight-segHeight;
  590. //5个像素间隔
  591. _userVideoMix = [[UIView alloc]initWithFrame:CGRectMake(0, 75+statusbarHeight, _window_width, mixHeight)];
  592. _segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, _window_width, segHeight)];
  593. NSArray *titleArray = @[YZMsg(@"用户"), YZMsg(@"视频")];
  594. _segmentedControl.sectionTitles = titleArray;
  595. _segmentedControl.selectedSegmentIndex = 0;
  596. _segmentedControl.selectionIndicatorHeight = 2.0;
  597. _segmentedControl.backgroundColor = UIColor.whiteColor;
  598. _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
  599. _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
  600. _segmentedControl.selectionIndicatorColor = [UIColor blackColor];
  601. _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
  602. _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
  603. _segmentedControl.tag = 3;
  604. __weak typeof(self) weakSelf = self;
  605. [_segmentedControl setIndexChangeBlock:^(NSInteger index) {
  606. [weakSelf.bgScrollView scrollRectToVisible:CGRectMake(_window_width * index, 0, _window_width, scrollHeight) animated:YES];
  607. }];
  608. [_userVideoMix addSubview:_segmentedControl];
  609. _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segHeight, _window_width, scrollHeight)];
  610. _bgScrollView.backgroundColor = UIColor.whiteColor;
  611. _bgScrollView.pagingEnabled = YES;
  612. _bgScrollView.showsHorizontalScrollIndicator = NO;
  613. _bgScrollView.contentSize = CGSizeMake(_window_width *titleArray.count , scrollHeight);
  614. _bgScrollView.delegate = self;
  615. [_bgScrollView scrollRectToVisible:CGRectMake(0, 0, _window_width, scrollHeight) animated:NO];
  616. [_userVideoMix addSubview:_bgScrollView];
  617. //用户
  618. _userTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, _window_width, scrollHeight) style:UITableViewStylePlain];
  619. _userTableView.delegate = self;
  620. _userTableView.dataSource = self;
  621. _userTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  622. _userTableView.backgroundColor = UIColor.whiteColor;
  623. _bgScrollView.pagingEnabled = YES;
  624. _userTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  625. _userPaging = 1;
  626. [self getUsers];
  627. }];
  628. _userTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  629. _userPaging += 1;
  630. [self getUsers];
  631. }];
  632. [_bgScrollView addSubview:_userTableView];
  633. //视频
  634. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
  635. flow.scrollDirection = UICollectionViewScrollDirectionVertical;
  636. flow.itemSize = CGSizeMake(_window_width/2-1, (_window_width/2-1) * 1.4);
  637. flow.minimumLineSpacing = 2;
  638. flow.minimumInteritemSpacing = 2;
  639. _videoCollectinView = [[UICollectionView alloc]initWithFrame:CGRectMake(_window_width, 0, _window_width, scrollHeight) collectionViewLayout:flow];
  640. [_videoCollectinView registerNib:[UINib nibWithNibName:@"VideoCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"VideoCollectionCell"];
  641. _videoCollectinView.delegate =self;
  642. _videoCollectinView.dataSource = self;
  643. _videoCollectinView.backgroundColor = UIColor.whiteColor;
  644. _videoCollectinView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  645. _videoPaging ++;
  646. [self getVideoLists];
  647. }];
  648. _videoCollectinView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  649. _videoPaging = 1;
  650. [self getVideoLists];
  651. }];
  652. [_bgScrollView addSubview:_videoCollectinView];
  653. }
  654. return _userVideoMix;
  655. }
  656. /*
  657. -(UITableView *)userTableView {
  658. if (!_userTableView) {
  659. //5个像素间隔
  660. _userTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 75+statusbarHeight, _window_width, _window_height-75-statusbarHeight-ShowDiff) style:UITableViewStylePlain];
  661. _userTableView.delegate = self;
  662. _userTableView.dataSource = self;
  663. _userTableView.separatorStyle = UITableViewCellStyleDefault;
  664. _userTableView.backgroundColor =Normal_Color;
  665. _userTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  666. _userPaging = 1;
  667. [self getUsers];
  668. }];
  669. _userTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  670. _userPaging += 1;
  671. [self getUsers];
  672. }];
  673. }
  674. return _userTableView;
  675. }
  676. - (UICollectionView *)videoCollectinView {
  677. if (!_videoCollectinView) {
  678. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
  679. flow.scrollDirection = UICollectionViewScrollDirectionVertical;
  680. flow.itemSize = CGSizeMake(_window_width/2-1, (_window_width/2-1) * 1.4);
  681. flow.minimumLineSpacing = 2;
  682. flow.minimumInteritemSpacing = 2;
  683. _videoCollectinView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 75+statusbarHeight, _window_width, _window_height-75-statusbarHeight-ShowDiff) collectionViewLayout:flow];
  684. [_videoCollectinView registerNib:[UINib nibWithNibName:@"VideoCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"VideoCollectionCell"];
  685. _videoCollectinView.delegate =self;
  686. _videoCollectinView.dataSource = self;
  687. _videoCollectinView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  688. _videoPaging ++;
  689. [self getVideoLists];
  690. }];
  691. _videoCollectinView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  692. _videoPaging = 1;
  693. [self getVideoLists];
  694. }];
  695. }
  696. return _videoCollectinView;
  697. }
  698. */
  699. /*
  700. //加上 搜索栏
  701. searchBars = [[HXSearchBar alloc] initWithFrame:CGRectMake(10,10+statusbarHeight, self.view.frame.size.width -20,60)];
  702. searchBars.backgroundColor = UIColor.redColor;//CellRow_Cor;
  703. searchBars.delegate = self;
  704. //输入框提示
  705. searchBars.placeholder = YZMsg(@"搜索用户|视频");
  706. //光标颜色
  707. searchBars.cursorColor = [UIColor whiteColor];
  708. //TextField
  709. searchBars.searchBarTextField.layer.cornerRadius = 18;
  710. searchBars.searchBarTextField.layer.masksToBounds = YES;
  711. //searchBars.searchBarTextField.layer.borderColor = [UIColor grayColor].CGColor;
  712. //searchBars.searchBarTextField.layer.borderWidth = 1.0;
  713. searchBars.searchBarTextField.backgroundColor = RGB_COLOR(@"#201F37", 1);
  714. searchBars.searchBarTextField.textColor = [UIColor whiteColor];
  715. searchBars.searchBarTextField.font = [UIFont systemFontOfSize:14];
  716. //清除按钮图标
  717. //searchBar.clearButtonImage = [UIImage imageNamed:@"demand_delete"];
  718. UIButton *clearBtn = [searchBars.searchBarTextField valueForKey:@"_clearButton"];
  719. [clearBtn addTarget:self action:@selector(clickClearBtn) forControlEvents:UIControlEventTouchUpInside];
  720. //去掉取消按钮灰色背景
  721. searchBars.hideSearchBarBackgroundImage = YES;
  722. [searchBars becomeFirstResponder];
  723. [bg addSubview:searchBars];
  724. */
  725. //#pragma mark - 搜索代理
  726. //- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
  727. // HXSearchBar *sear = (HXSearchBar *)searchBar;
  728. // sear.cancleButton.backgroundColor = [UIColor clearColor];
  729. // [sear.cancleButton setTitle:YZMsg(@"取消") forState:UIControlStateNormal];
  730. // [sear.cancleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  731. // sear.cancleButton.titleLabel.font = [UIFont systemFontOfSize:16];
  732. //
  733. // if (searchBar.text.length>0) {
  734. // isHistory = NO;
  735. // _historyTableView.hidden = YES;
  736. // _userVideoMix.hidden = NO;
  737. // }else{
  738. // isHistory = YES;
  739. // _historyTableView.hidden = NO;
  740. // _userVideoMix.hidden = YES;
  741. // }
  742. //}
  743. ////文字改变
  744. //-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  745. // if (searchText.length>0) {
  746. // isHistory = NO;
  747. // _historyTableView.hidden = YES;
  748. // _userVideoMix.hidden = NO;
  749. // [_userArray removeAllObjects];
  750. // [_userTableView reloadData];
  751. //
  752. // _userPaging = 1;
  753. // _videoPaging = 1;
  754. //
  755. // [self getUsers];
  756. //
  757. // [self getVideoLists];
  758. //
  759. // }else{
  760. // isHistory = YES;
  761. // _historyTableView.hidden = NO;
  762. // _userVideoMix.hidden = YES;
  763. // [self refreshHistory];
  764. // }
  765. //}
  766. //取消按钮点击的回调
  767. //- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  768. // searchBar.showsCancelButton = NO;
  769. // searchBar.text = nil;
  770. // [self.view endEditing:YES];
  771. // [self.navigationController popViewControllerAnimated:YES];
  772. // [self dismissViewControllerAnimated:YES completion:nil];
  773. //}
  774. //清除按钮
  775. //-(void)clickClearBtn {
  776. // isHistory = YES;
  777. // _userVideoMix.hidden = YES;
  778. // _historyTableView.hidden = NO;
  779. // [self refreshHistory];
  780. //}
  781. @end