Custom KeyBoard Use UITableView Swift 4 Xcode 9



This how use XIB file include custom keyboard with UIview

  •  MainViewController



  import UIKit

class KeyBoardViewController: UIViewController, KeyboardLDelegate {
    func didSelect(data: PickerDataSource1?) {
        a2.insertText("hi D")
    }
    
    @IBOutlet var A1: UITextField!
    @IBOutlet var a2: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func hendleSelect(_ sender: Any) {
        let keyboardView = keyviewF(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
        keyboardView.delegate = self
        keyboardView.resignFirstResponder()
        a2.inputView = keyboardView
    }
    
    func buttonTapped() {
        a2.resignFirstResponder()
    }
}


  •  keyBoardUIView


 import UIKit

struct PickerDataSource1 {
    var id: String
    var name: String
}

protocol KeyboardLDelegate: class {
    func didSelect(data: PickerDataSource1?)
}

class keyviewF: UIView, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet var hiQ: UIView!
    weak var delegate: KeyboardLDelegate?
    @IBOutlet weak var tableView: UITableView!
    var selectData: PickerDataSource1?
    var dataPicker: [PickerDataSource1] = []
    
    @IBAction func bAction(_ sender: UIButton) {
        self.delegate?.didSelect(data: self.selectData)
        self.removeFromSuperview()
    }
    
    
    //Original
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initializeSubviews()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeSubviews()
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 50.0
        self.tableView.separatorInset = UIEdgeInsetsMake(0, UIScreen.main.bounds.width, 0, 0)
        self.tableView.register(UINib(nibName: "ApiTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
    }
    
    func initializeSubviews() {
        Bundle.main.loadNibNamed("keyView", owner: self, options: nil)
        addSubview(hiQ)
        hiQ.frame = self.bounds
        hiQ.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataPicker.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ApiTableViewCell
        cell.merkLabel.text = self.dataPicker[indexPath.row].name
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.selectData = self.dataPicker[indexPath.row]
        if let cell = tableView.cellForRow(at: indexPath) as? ApiTableViewCell {
            cell.accessoryType = .checkmark
        }
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? ApiTableViewCell {
            cell.accessoryType = .none
        }
    }
    
    
}


 Link download source code.


This my note as my problem and solution @luffyselah

No comments: