<commit_before>//
//  GFDropDown.swift
//  GFDropDownDemo
//
//  Created by ECHINACOOP1 on 2017/4/1.
//  Copyright © 2017年 蔺国防. All rights reserved.
//

import UIKit

let cellId = "cellID"

class GFDropDown: UIView, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

    var tableView : UITableView?
    var textField : UITextField?
    
    ///textFiled高度&&控件的高度
    var textFieldH: CGFloat?
    ///tableView高度
    var tableViewH: CGFloat? = 120
    ///tableView的cell高度
    var rowH      : CGFloat? = 50
    ///数据源
    var data      : [Any]?
    ///占位文字
    var placeText : String?
    ///列表是否显示
    var isShowList: Bool?{
    
        didSet{
            if isShowList! {
                //设置下拉动画
                UIView.beginAnimations("ResizeForKeyBorard", context: nil
                )
                UIView.setAnimationCurve(UIViewAnimationCurve.easeIn)
                self.frame.size.height = textFieldH! + tableViewH!
                tableView?.frame.size.height = tableViewH!
                UIView.commitAnimations()
            }else{
                //设置上拉动画
                UIView.beginAnimations("ResizeForKeyBorard", context: nil
                )
                UIView.setAnimationCurve(UIViewAnimationCurve.easeIn)
                self.frame.size.height = textFieldH!
                tableView?.frame.size.height = 0
                UIView.commitAnimations()
            }
        }
    }
    
    init(frame: CGRect, dataArr:[Any], placeHolder: String) {
        
        super.init(frame: frame);
        
        data = dataArr
        placeText = placeHolder
        textFieldH = frame.size.height
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        //创建并设置textfield和tableView
        textField = UITextField.init(frame: CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: textFieldH!))
        
        textField?.delegate = self
        textField?.placeholder = placeText
        textField?.layer.borderWidth = 1
        textField?.layer.masksToBounds = true
        textField?.layer.cornerRadius = 5
        textField?.layer.borderColor = UIColor.gray.cgColor
        textField?.placeholder = placeText
        
        tableView = UITableView.init(frame: CGRect.init(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 0))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        tableView?.delegate = self
        tableView?.rowHeight = rowH!
        tableView?.dataSource = self
        
        tableView?.layer.cornerRadius = 5
        tableView?.layer.masksToBounds = true
        tableView?.layer.borderWidth = 1
        tableView?.layer.borderColor = UIColor.gray.cgColor
        
        self.addSubview(textField!)
        self.addSubview(tableView!)
        
        //将View提前防止下拉后被遮挡
        self.superview?.bringSubview(toFront: self)
        
        //初始化不显示列表
        isShowList = false
    }
    
    //MARK: - UITextFieldDelegate
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
        
        //控制列表显示状态
        if self.isShowList! {
            
            self.isShowList = false<commit_msg>修复下拉遮挡bug<commit_after>//
//  GFDropDown.swift
//  GFDropDownDemo
//
//  Created by ECHINACOOP1 on 2017/4/1.
//  Copyright © 2017年 蔺国防. All rights reserved.
//

import UIKit

let cellId = "cellID"

class GFDropDown: UIView, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

    var tableView : UITableView?
    var textField : UITextField?
    
    ///textFiled高度&&控件的高度
    var textFieldH: CGFloat?
    ///tableView高度
    var tableViewH: CGFloat? = 120
    ///tableView的cell高度
    var rowH      : CGFloat? = 50
    ///数据源
    var data      : [Any]?
    ///占位文字
    var placeText : String?
    ///列表是否显示
    var isShowList: Bool?{
    
        didSet{
            if isShowList! {
                
                self.superview?.bringSubview(toFront: self)
                //设置下拉动画
                UIView.beginAnimations("ResizeForKeyBorard", context: nil
                )
                UIView.setAnimationCurve(UIViewAnimationCurve.easeIn)
                self.frame.size.height = textFieldH! + tableViewH!
                tableView?.frame.size.height = tableViewH!
                UIView.commitAnimations()
            }else{
                //设置上拉动画
                UIView.beginAnimations("ResizeForKeyBorard", context: nil
                )
                UIView.setAnimationCurve(UIViewAnimationCurve.easeIn)
                self.frame.size.height = textFieldH!
                tableView?.frame.size.height = 0
                UIView.commitAnimations()
            }
        }
    }
    
    init(frame: CGRect, dataArr:[Any], placeHolder: String) {
        
        super.init(frame: frame);
        
        data = dataArr
        placeText = placeHolder
        textFieldH = frame.size.height
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        //创建并设置textfield和tableView
        textField = UITextField.init(frame: CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: textFieldH!))
        
        textField?.delegate = self
        textField?.placeholder = placeText
        textField?.layer.borderWidth = 1
        textField?.layer.masksToBounds = true
        textField?.layer.cornerRadius = 5
        textField?.layer.borderColor = UIColor.gray.cgColor
        textField?.placeholder = placeText
        
        tableView = UITableView.init(frame: CGRect.init(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: 0))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        tableView?.delegate = self
        tableView?.rowHeight = rowH!
        tableView?.dataSource = self
        
        tableView?.layer.cornerRadius = 5
        tableView?.layer.masksToBounds = true
        tableView?.layer.borderWidth = 1
        tableView?.layer.borderColor = UIColor.gray.cgColor
        
        self.addSubview(textField!)
        self.addSubview(tableView!)
        
        //初始化不显示列表
        isShowList = false
    }
    
    //MARK: - UITextFieldDelegate
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
        
        //控制列表显示状态
        if self.isShowList! {
            
            self.isShowList = false
{'fraction_non_alphanumeric': 0.09616555082166768, 'fraction_numerical': 0.007608034083992696, 'mean_word_length': 1.8258813413585555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7382867', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2037, 'n_words': 358}