MoreTVStationViewController.swift 6.82 KB
//
//  MoreTVStationViewController.swift
//  YouerLiveVideo
//
//  Created by 左丞 on 2017/5/16.
//  Copyright © 2017年 左丞. All rights reserved.
//

import UIKit

class MoreTVStationViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    var topTenTVStation:[TVStationInfor] = []
    var xxcellentSchool:[TVStationInfor] = []
    var excellentTVStationProgram:[TVStationSubject] = []
    var type:Int = 0 //0:十佳电视台,1:优秀电视台,2:优秀校园节目
    var pageIndex:Int = 2
    override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false
        if type != 0{
            addCellAndRefresh()
        }
        // Do any additional setup after loading the view.
    }

    func addCellAndRefresh(){
        collectionView.mj_header=MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(MoreTVStationViewController.loadNewData))
        
        collectionView.mj_footer=MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(MoreTVStationViewController.loadMoreData))
    }
    
    // MARK: - 下拉刷新
    func loadNewData(){
        pageIndex=1
        getTVStation() { (finish) in
            self.collectionView.mj_header.endRefreshing()
        }
    }
    // MARK: - 上拉加载更多
    func loadMoreData(){
        getTVStation() { (finish) in
            self.collectionView.mj_footer.endRefreshing()
        }
    }
    
    func getTVStation(finish: @escaping (Bool)->()){
        if type == 1{
            AppDelegate.instance().httpServer.postExcellentSchool(parameters: ["pageIndex":pageIndex as AnyObject,"pageSize":8 as AnyObject]) { (str, error) in
                httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                    if json.contentData().dictionaryValue["resultData"] != nil{
                        if self.pageIndex==1{
                            self.xxcellentSchool.removeAll()
                        }
                        for item in json.contentData().dictionaryValue["resultData"]!.arrayValue{
                            self.xxcellentSchool.append(TVStationInfor(json: item))
                        }
                        if json.contentData().dictionaryValue["resultData"]!.arrayValue.count > 0{
                            self.pageIndex=self.pageIndex+1
                        }
                        finish(true)
                        self.collectionView.reloadData()
                    }
                }, failHandler: { (error) in
                    finish(false)

                })
            }
        }else if type == 2{
            AppDelegate.instance().httpServer.postExcellentProgram(parameters: ["pageIndex":pageIndex as AnyObject,"pageSize":8 as AnyObject]) { (str, error) in
                httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                    if json.contentData().dictionaryValue["resultData"] != nil{
                        if self.pageIndex==1{
                            self.excellentTVStationProgram.removeAll()
                        }
                        for item in json.contentData().dictionaryValue["resultData"]!.arrayValue{
                            self.excellentTVStationProgram.append(TVStationSubject(json: item))
                        }
                        if json.contentData().dictionaryValue["resultData"]!.arrayValue.count > 0{
                            self.pageIndex=self.pageIndex+1
                        }
                        finish(true)
                        self.collectionView.reloadData()
                    }
                }, failHandler: { (error) in
                    finish(false)
                })
            }
        }
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension MoreTVStationViewController:UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if type == 2{
            let vc = Story.instantiateViewControllerWithIdentifier("TVStationInforViewControllerVC", storyName: "TVStation") as! TVStationInforViewController
            vc.excellentTVStationProgram = excellentTVStationProgram[indexPath.row]
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MoreTVStationViewCollectionCell
        if type == 0{
            cell.tvPicture.sd_setImage(with: URL(string: topTenTVStation[indexPath.row].f_Logo), placeholderImage: UIImage(named:"icon"))
            cell.tvStationName.text = topTenTVStation[indexPath.row].f_SchoolName
        }else if type == 1{
            cell.tvPicture.sd_setImage(with: URL(string: xxcellentSchool[indexPath.row].f_Logo), placeholderImage: UIImage(named:"icon"))
            cell.tvStationName.text = xxcellentSchool[indexPath.row].f_SchoolName
        }else if type == 2{
            cell.tvPicture.sd_setImage(with: URL(string: excellentTVStationProgram[indexPath.row].f_Img), placeholderImage: UIImage(named:"icon"))
            cell.tvStationName.text = excellentTVStationProgram[indexPath.row].f_Title
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        switch type {
        case 0:
            return topTenTVStation.count
        case 1:
            return xxcellentSchool.count
        case 2:
            return excellentTVStationProgram.count
        default:
            return 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(10, 10, 10, 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (getScreenWidth()-30)/2, height: 180)
    }

}

class MoreTVStationViewCollectionCell:UICollectionViewCell{
    
    @IBOutlet weak var tvStationName: UILabel!
    @IBOutlet weak var tvPicture: UIImageView!
    
}