SelectContentView.swift 3.86 KB
//
//  SelectContentView.swift
//  GeliBusinessPlatform
//
//  Created by 刘俊宏 on 2020/4/22.
//  Copyright © 2020 junye lu. All rights reserved.
//

import Foundation
protocol SelectContentViewDelegate {
    func didselectCell(sender:Int)
    func selectContentvRemoveForSup()
}
class SelectContentView: UIView, UITableViewDelegate, UITableViewDataSource {
    var delegate:SelectContentViewDelegate?

    @IBOutlet weak var tabContentHeight: NSLayoutConstraint!
    @IBOutlet weak var tabContentV: UIView!
    var contentView:UIView!
    
    var tableV :UITableView!
    
    var selectIndx : Int = 0 {
        didSet{
            tableV.selectRow(at: IndexPath(row: selectIndx, section: 0), animated: false, scrollPosition: .none)
        }
    }
    
    var dataArr : [String] = []{
        didSet{
            if dataArr.count > 3 {
                if dataArr.count > 8 {
                    tabContentHeight.constant = 50 * 7 * glscale + 5

                }else{
                    tabContentHeight.constant = CGFloat((50 * Int(glscale)) * dataArr.count + 5)
                }
            }else{
                tabContentHeight.constant = 150 * glscale + 5
            }
            tableV.reloadData()
        }
    }
 
    func setTabv() {
        tableV = UITableView()
        tabContentV.addSubview(tableV);
        tableV.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(5)
        }
        tableV.delegate = self
        tableV.dataSource = self
        tableV.separatorStyle = .none
        tableV?.register(UINib(nibName: "TitleContentCell", bundle: nil), forCellReuseIdentifier: "TitleContentCell")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TitleContentCell") as! TitleContentCell
        let titleStr = dataArr[indexPath.row]
        cell.contentLbl.text = ""
        cell.titleLbl.text = titleStr
        if selectIndx == indexPath.row {
            cell.titleLbl.textColor = UIColor.init(named: "蓝色字体颜色")
        }else{
            cell.titleLbl.textColor = UIColor.init(named: "标题字颜色")

        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.delegate?.didselectCell(sender: indexPath.row)
        self.removeFromSuperview()
    }
    
    @IBAction func balckTapAction(_ sender: UITapGestureRecognizer) {
        self.delegate?.selectContentvRemoveForSup()
        self.removeFromSuperview()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50*glscale
    }
    //初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalToSuperview()
        }
        setTabv()
    }
    //    /初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        contentView = loadViewFromNib()
        addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalToSuperview()
        }
        setTabv()
    }
    //加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }
}