RankingTableViewController.swift 10.1 KB
//
//  RankingTableViewController.swift
//  YouerLiveVideo
//
//  Created by 葛建军 on 17/5/20.
//  Copyright © 2017年 左丞. All rights reserved.
//

import UIKit

class RankingTableViewController: UITableViewController {
    var openIndexAry:[Int]=[0,4]        //记录那些是展开状态
    var allDic:[Int:[String]]=Dictionary<Int,[String]>()
    var ary:[String]=["综合排名","资源贡献","下载排名","阅读排名","综合排名","资源贡献","下载排名","阅读排名"]
    override func viewDidLoad() {
        super.viewDidLoad()
        getAllSubRankings()
    }
    func getAllSubRankings(){
        for i in 0..<8{
            allDic.updateValue([], forKey: i)
        }
        AppDelegate.instance().httpServer.getSchoolGeneral { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 0)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
        AppDelegate.instance().httpServer.getSchoolUpload { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 1)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
        AppDelegate.instance().httpServer.getSchoolDownloads { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 2)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
        AppDelegate.instance().httpServer.getSchoolPv { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 3)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
        AppDelegate.instance().httpServer.getTeacherGenerals { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 4)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
        AppDelegate.instance().httpServer.getTeacherUpload { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 5)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
        AppDelegate.instance().httpServer.getTeacherDownload { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 6)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
        AppDelegate.instance().httpServer.getTeacherPvs { (str, error) in
            if error==nil {
                if JSON.fromString(jsonString: str)!["status"].intValue == 1{
                    httpJsonResule(jsonString: str, error: error, successHandler: { (json) in
                        var lineAry:[String]=[]
                        for item in JSON.fromString(jsonString: str)!["data"].arrayValue {
                            lineAry.append(item["f_SchoolName"].stringValue)
                        }
                        self.allDic.updateValue(lineAry, forKey: 7)
                        self.tableView.reloadData()
                    }, failHandler: { (error) in
                    })
                }
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var height:CGFloat=44
        if indexPath.row==0 || indexPath.row==4{
            height=height+44
        }
        if indexPath.row != 3{
            height=height+20
        }
        if openIndexAry.contains(indexPath.row){
            height=height+CGFloat(30*allDic[indexPath.row]!.count)
        }
        return height
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=tableView.dequeueReusableCell(withIdentifier: "ItemTableViewCell", for: indexPath) as! ItemTableViewCell
        cell.superVC=self
        cell.indexPath=indexPath
        if indexPath.row==0{
            cell.sectionTitleLabel.text="学校排行"
            cell.sectionTitleLabel.isHidden=false
        }
        if indexPath.row==4{
            cell.sectionTitleLabel.text="教师排行"
            cell.sectionTitleLabel.isHidden=false
        }
        if indexPath.row==0 || indexPath.row==4{
            cell.titleViewTop.constant=44
        }else{
            cell.titleViewTop.constant=0
        }
        if indexPath.row==3{
            cell.tableViewButtom.constant=0
        }else{
            cell.tableViewButtom.constant=20
        }
        cell.titleLabel.text=ary[indexPath.row]
        if openIndexAry.contains(indexPath.row){
            cell.titleImageView.image=UIImage(named: "icon_open")
        }else{
            cell.titleImageView.image=UIImage(named: "icon_more")
        }
        cell.subTableView.reloadData()
        return cell
    }
    
}
class ItemTableViewCell: UITableViewCell,UITableViewDelegate,UITableViewDataSource {
    var superVC:RankingTableViewController?
    var indexPath:IndexPath!
    @IBOutlet var sectionTitleLabel: UILabel!
    @IBOutlet var titleViewTop: NSLayoutConstraint!
    @IBOutlet var subTableView: UITableView!
    @IBOutlet var tableViewButtom: NSLayoutConstraint!
    @IBOutlet var titleLabel: UILabel!
    @IBOutlet var titleImageView: UIImageView!
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if superVC != nil {
            if superVC!.openIndexAry.contains(indexPath.row){
                return superVC!.allDic[indexPath.row]!.count
            }else{
                return 0
            }
        }else{
            return 0
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 30
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        if superVC != nil {
            cell.textLabel?.text="\(superVC!.allDic[self.indexPath.row]![indexPath.row])"
        }
        return cell
    }
    @IBAction func openOrCloseAction(_ sender: UIButton) {
        if superVC!.openIndexAry.contains(indexPath.row){
            superVC!.openIndexAry.remove(at: getCurrentIndex(item: indexPath.row, arry: superVC!.openIndexAry))
        }else{
            superVC!.openIndexAry.append(indexPath.row)
        }
        superVC!.tableView.reloadData()
    }
}