自定義帶DropDownTable的TextField(事件)

上節已經把tableview成功的在uitextview的下面顯示出來了。swift

如今須要給這個tableview添加點擊DataSource和DataDelegate函數

先定義一個協議,用於外部ViewController傳入數據和獲取點擊事件,函數做用顧名思義。ui

public protocol DropDownTextFiledDataSourceDelegate:NSObjectProtocol{
    func dropDownTextField(dropDownTextField:DropDownTextField,numberOfRowsInSection section:Int)->Int
    func dropDownTextField(dropDownTextField:DropDownTextField,cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell
    func dropDownTextField(dropDownTextField:DropDownTextField,didSelectRowAtIndexPath indexPath: NSIndexPath)
}

在UITextField的tableview代理函數中調用外部傳入的該代理實例。spa

public weak var dataSourceDelegate: ZTDropDownTextFieldDataSourceDelegate?

extension DropDownTextField:UITableViewDataSource,UITableViewDelegate{
    
    @available(iOS 2.0, *)
     public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if let dataSourceDelegate = dataSourceDelegate {
            if dataSourceDelegate.respondsToSelector(Selector("dropDownTextField:numberOfRowsInSection:")) {
                return dataSourceDelegate.dropDownTextField(self, numberOfRowsInSection: section)
            }
        }
        return 0

    }
    
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
    
    @available(iOS 2.0, *)
     public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        
        if let dataSourceDelegate = dataSourceDelegate {
            if dataSourceDelegate.respondsToSelector(Selector("dropDownTextField:cellForRowAtIndexPath:")) {
                return dataSourceDelegate.dropDownTextField(self, cellForRowAtIndexPath: indexPath)
            }
        }
        return UITableViewCell()
        
    }
  public   
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        if let dataSourceDelegate = dataSourceDelegate {
            if dataSourceDelegate.respondsToSelector(Selector("dropDownTextField:didSelectRowAtIndexPath:")) {
                dataSourceDelegate.dropDownTextField(self, didSelectRowAtIndexPath: indexPath)
            }
        }

              self.dropTable.hidden = true 代理

    }

    
}

  

在外部ViewController中實現自定義的協議,而且將ViewController自身傳遞給DropDownTextFiled。blog

 dropTextField.dataSourceDelegate = self
extension ViewController: DropDownTextFiledDataSourceDelegate {
    func dropDownTextField(dropDownTextField: DropDownTextField, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func dropDownTextField(dropDownTextField: DropDownTextField, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let reuseIdentifier = "dropdownCell"
        var cell: UITableViewCell? = dropDownTextField.dropTable.dequeueReusableCellWithIdentifier(reuseIdentifier)
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: reuseIdentifier)
        }
        
        cell!.textLabel!.text = "hello"
        cell!.textLabel?.numberOfLines = 0
        
        return cell!
    }
    
    func dropDownTextField(dropdownTextField: DropDownTextField, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        resultLabel.text = "\(indexPath.row)"
    }
}

  至此,ui和數據操做都完成了事件

相關文章
相關標籤/搜索