// // InformationViewController.swift // ParentAssistant // // Created by 葛建军 on 2018/3/5. // Copyright © 2018年 HANGZHOUTEAM. All rights reserved. // import UIKit class InformationViewController: UIViewController,UISearchBarDelegate { @IBOutlet weak var collectView: UICollectionView! var leftLabel:UILabel! var data:ChannelDemol! var topADs:[ADSModel]=[] var isFirstviewController:Bool=true var channel:Int=0 override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.barStyle = UIBarStyle.default// UIColorFromRGB(0xC5DAFF) let backImage = UIImage(named: "navigationBar_backgrounImage") self.navigationController?.navigationBar.setBackgroundImage(backImage, for: UIBarMetrics.default) // Do any additional setup after loading the view. if isFirstviewController{ self.navigationItem.title = "" addLeftBarBtn() }else{ } let searchView = UISearchBar(frame: CGRect(x: 0, y: 7, width: getScreenWidth()-40, height: 30)) searchView.delegate = self searchView.placeholder = "请输入搜索内容" searchView.showsCancelButton = false self.navigationItem.titleView = searchView self.navigationController?.navigationBar.tintColor=UIColor.white } func getData(){ let topADsQueue = DispatchQueue(label: "topADs") topADsQueue.async { //position 广告位置0-首次广告位1-开屏广告位2-资讯首页3-频道首页4-频道内容5-文章内容页 var position:Int = 2 if !self.isFirstviewController { position = 3 } HTTPServer.shared.getAds(["position":position as AnyObject,"areaName":AccountManager.shared.address as AnyObject], completionHandler: { (str, error) in//getPercentEncodingString(str: AccountManager.shared.address) httpJsonResule(jsonString: str, error: error, successHandler: { (json) in if json["status"] == 1 && error == nil{ self.topADs.removeAll() for item in json.contentData().arrayValue{ self.topADs.append(ADSModel(j: item)) } self.collectView.reloadData() } }, failHandler: { (error) in }) }) } topADsQueue.async { let dic = self.getListDic() HTTPServer.shared.getChannelInfo(dic, completionHandler: { (str, error) in httpJsonResule(jsonString: str, error: error, successHandler: { (json) in if json["status"] == 1 && error == nil{ self.data = ChannelDemol(j: json.contentData()) self.collectView.reloadData() } }, failHandler: { (error) in }) }) } } func getList(key:String?) { if key == nil{ return } var dic = getListDic() dic.updateValue(key as AnyObject, forKey: "keyword") HTTPServer.shared.getInformationTopic(dic, completionHandler: { (str, error) in httpJsonResule(jsonString: str, error: error, successHandler: { (json) in if json["status"] == 1 && error == nil{ self.data.channelContent.removeAll() for item in json.contentData()["channelList"].arrayValue{ self.data.channelContent.append(ChannelContentDemol(j: item)) } self.collectView.reloadData() } }, failHandler: { (error) in }) }) } //获得请求参数 func getListDic()->[String:AnyObject]{ var dic = [ "areaName": AccountManager.shared.address as AnyObject,"pageIndex": 1 as AnyObject,"pageSize": 20 as AnyObject] var toFirstPage:Int = 1 var ch:Int = 0 if !isFirstviewController{ toFirstPage = 0 ch = channel } dic.updateValue(ch as AnyObject, forKey: "channel") dic.updateValue(toFirstPage as AnyObject, forKey: "toFirstPage") return dic } //左上交城市选择按钮添加 func addLeftBarBtn(){ let leftbtn = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40)) leftLabel = UILabel(frame: CGRect(x: 0, y: 10, width: 60, height: 20)) leftLabel.text = AccountManager.shared.address leftLabel.textColor = UIColor.white leftLabel.sizeToFit() let image = UIImageView(frame: CGRect(x: leftLabel.frame.maxX+4, y: 15, width: 16, height: 9)) image.image = #imageLiteral(resourceName: "evaluation_icon_pushDown") leftbtn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(EvaluationViewController.getAddreess(tap:)))) leftbtn.addSubview(leftLabel) leftbtn.addSubview(image) leftbtn.frame.size = CGSize(width: image.frame.maxX+4, height: 40) self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftbtn) } //地址选择 @objc func getAddreess(tap:UITapGestureRecognizer){ tap.view?.isUserInteractionEnabled = false LZCityPickerController.showPicker(in: self , select: { (address, province, city, area, isSelect) in if isSelect{ tap.view?.isUserInteractionEnabled = true self.selectCity(city: area!) } }) { tap.view?.isUserInteractionEnabled = true } } //完成选择城市 func selectCity(city: String) { AccountManager.shared.address = city addLeftBarBtn() getData() } func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { searchBar.showsCancelButton = true } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchData(searchBar) } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchData(searchBar) } //搜索资讯 func searchData(_ searchBar: UISearchBar){ getList(key: searchBar.text) searchBar.resignFirstResponder() searchBar.showsCancelButton = false } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) getData() if isFirstviewController{ leftLabel.text = AccountManager.shared.address }else{ } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //点击资讯进入详情 func pushToInfoView(index:Int){ let vcs = Story.instantiateViewControllerWithIdentifier("JSViewController", storyName: "JS") as! JSViewController if data.channelContent[index].url != "" { vcs.webURL = data.channelContent[index].url }else{ vcs.webURL = (HTTPServer.HOSTWAP+"/InformationDetail.aspx?id="+"\(data.channelContent[index].id)"+"&areaname="+AccountManager.shared.address).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! } self.navigationController?.present(vcs.wrapWithNavigationController(), animated: true, completion: nil) } } extension InformationViewController:XRCarouselViewDelegate{ func carouselView(_ carouselView: XRCarouselView!, clickImageAt index: Int) { pushToInfoView(index: index) } } //整体分三个部分1.轮播图,2频道栏,3列表 extension InformationViewController:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { switch section { case 0: return 1 case 1: if data != nil && data.subchannel.count > 0{ return 1 } return 0 default: return data != nil ? data.channelContent.count : 0 } } func numberOfSections(in collectionView: UICollectionView) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch indexPath.section { case 0: return CGSize(width: getScreenWidth(), height: getScreenWidth()/375*200) case 1: return CGSize(width: getScreenWidth()-20, height: 92) default: // if indexPath.row == 0{ return CGSize(width: getScreenWidth()-20, height: 92) // } // return CGSize(width: getScreenWidth()-20, height: 180) } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { if section == 2{ return CGSize(width: getScreenWidth()-20, height: 40) }else{ return CGSize(width: getScreenWidth(), height: 10) } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { collectionView.register(UINib(nibName: "ChannelCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "contentCell") switch indexPath.section { case 0: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topCell", for: indexPath) as! InformationViewTopCell if topADs.count > 0 { var arr:[String]=[] for item in topADs{ arr.append(HTTPServer.HOSTImage+item.fileSrc) } cell.headerImageView.imageArray = arr } return cell case 1: let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "channelSelectCell", for: indexPath) as! ChannelSelectView cell.SubchannelList = data.subchannel cell.superNC = self.navigationController return cell default: // if indexPath.row == 0{ let item = data.channelContent[indexPath.row] let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "contentCell", for: indexPath) as! ChannelCollectionViewCell cell.title.text = item.title cell.option.text = item.content cell.optionNum.text = "\(item.lookNum)" cell.shareNum.text = "\(item.forwardingNum)" cell.picture.sd_setImage(with: URL(string: (HTTPServer.HOSTImage+item.image).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!), placeholderImage: UIImage(named: "navigationBar_backgrounImage")) return cell // }else{ // let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adiouPlayerCell", for: indexPath) as! AdiouPlayerCell // return cell // } } } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { switch indexPath.section { case 1: let vc = Story.instantiateViewControllerWithIdentifier("InformationViewControllerVC", storyName: "Main") as! InformationViewController vc.channel = data.subchannel[indexPath.row].id vc.isFirstviewController = false self.navigationController?.pushViewController(vc, animated: true) break default: let vcs = Story.instantiateViewControllerWithIdentifier("JSViewController", storyName: "JS") as! JSViewController if data.channelContent[indexPath.row].url != "" { vcs.webURL = data.channelContent[indexPath.row].url }else{ vcs.webURL = (HTTPServer.HOSTWAP+"/InformationDetail.aspx?id="+"\(data.channelContent[indexPath.row].id)"+"&areaname="+AccountManager.shared.address).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! } self.navigationController?.present(vcs.wrapWithNavigationController(), animated: true, completion: nil) // pushToInfoView(index: indexPath.row) } } func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { var reusableview:UICollectionReusableView? if kind == UICollectionElementKindSectionHeader{ let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView", for: indexPath) as! InformationSectionHeader headerView.title.text = "咨讯话题" reusableview = headerView } if indexPath.section == 2{ reusableview?.isHidden = false }else{ reusableview?.isHidden = true } return reusableview! } } class InformationSectionHeader: UICollectionReusableView { @IBOutlet weak var title: UILabel! } //首页的轮播图所在的cell class InformationViewTopCell: UICollectionViewCell { @IBOutlet weak var headerImageView: XRCarouselView! } //视频cell class AdiouPlayerCell: UICollectionViewCell { } //频道栏 class ChannelSelectView: UICollectionViewCell { var superNC:UINavigationController! var SubchannelList:[SubchannelDemol]=[] @IBOutlet weak var channelSelectCollectionView: UICollectionView! override func awakeFromNib() { channelSelectCollectionView.register(UINib(nibName: "ChannelSelectViewCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "channelcell") } } extension ChannelSelectView:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return SubchannelList.count } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: (getScreenWidth()-20)/4, height: 92) } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "channelcell", for: indexPath) as! ChannelSelectViewCollectionViewCell cell.image.sd_setImage(with: URL(string: (HTTPServer.HOSTImage+SubchannelList[indexPath.row].channelImage).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!), placeholderImage: UIImage(named: "evaluation_icon_highSchoolEnter")) cell.channelName.text = SubchannelList[indexPath.row].channelName return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let vc = Story.instantiateViewControllerWithIdentifier("InformationViewControllerVC", storyName: "Main") as! InformationViewController vc.channel = SubchannelList[indexPath.row].id vc.isFirstviewController = false superNC.pushViewController(vc, animated: true) } } class ChannelDemol:NSObject{ var id:Int=0 // 频道ID , var channelName:String=""// 频道名称 , var channelImage:String=""// 频道图片 , var orderById:Int=0 // 频道排序 , var subchannel:[SubchannelDemol] = []// 二级频道 , var channelContent:[ChannelContentDemol] = []// 频道信息 init(j:JSON){ id = j["id"].intValue channelName = j["channelName"].stringValue channelName = j["channelName"].stringValue orderById = j["orderById"].intValue for item in j["subchannel"].arrayValue { subchannel.append(SubchannelDemol(j: item)) } for item in j["channelContent"].arrayValue { channelContent.append(ChannelContentDemol(j: item)) } } } class SubchannelDemol: NSObject { var id:Int=0 // 二级频道ID , var channelName:String=""// 二级频道名称 , var channelImage:String=""// 二级频道图片 , var orderById:Int=0// 二级工具排序, var url:String=""// 二级工具链接 init(j:JSON){ id = j["id"].intValue channelName = j["channelName"].stringValue channelImage = j["channelImage"].stringValue orderById = j["orderById"].intValue url = j["url"].stringValue } } class ChannelContentDemol: NSObject { var id:Int=0// 资讯ID , var image:String=""// 资讯标题图片 , var title:String=""// 资讯标题 , var url:String=""// 资讯链接 , var content:String=""// 资讯内容 , var forwardingNum:Int=0// 资讯转发数 , var talkNum:Int=0 // 评论数 , var lookNum:Int = 0 //浏览数 var orderById:Int=0// 资讯排序 var isAds:Int=0// 是否为广告 1是广告 init(j:JSON){ id = j["id"].intValue image = j["image"].stringValue title = j["title"].stringValue url = j["url"].stringValue content = j["content"].stringValue forwardingNum = j["forwardingNum"].intValue talkNum = j["talkNum"].intValue lookNum = j["lookNum"].intValue orderById = j["orderById"].intValue isAds = j["isAds"].intValue } } class ADSModel: NSObject { var id:Int=0// 广告ID , var location:Int=0// 广告位 , var describe:String=""// 广告内容 , var fileSrc:String=""// 广告内容 , var url:String=""// 广告跳转链接 , var orderById:Int=0// 广告排序 init(j:JSON){ id = j["id"].intValue location = j["location"].intValue describe = j["describe"].stringValue fileSrc = j["fileSrc"].stringValue url = j["url"].stringValue orderById = j["orderById"].intValue } }