码迷,mamicode.com
首页 > 其他好文 > 详细

自定义带DropDownTable的TextField(事件)

时间:2016-01-25 16:53:51      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

上节已经把tableview成功的在uitextview的下面显示出来了。

现在需要给这个tableview添加点击DataSource和DataDelegate

先定义一个协议,用于外部ViewController传入数据和获取点击事件,函数作用顾名思义。

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代理函数中调用外部传入的该代理实例。

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。

 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和数据操作都完成了

 

自定义带DropDownTable的TextField(事件)

标签:

原文地址:http://www.cnblogs.com/mogul/p/5157699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!