zoukankan      html  css  js  c++  java
  • 自定义带DropDownTable的TextField(事件)

    上节已经把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和数据操作都完成了

    I am not doing shit today !
  • 相关阅读:
    Base4.net和IronPython的一些相关东东
    WPF E 文章汇总
    Novell 发布Mono 1.2 推动.NET跨平台
    Google真好,这么多的工具……
    bootstrap源码学习与示例:bootstrappopover
    bootstrap源码学习与示例:bootstrapscrollspy
    bootstrap源码学习与示例:bootstrapcollapse
    bootstrap源码学习与示例:bootstrapaffix
    bootstrap源码学习与示例:bootstraptab
    mass Framework attr模块 v3
  • 原文地址:https://www.cnblogs.com/mogul/p/5157699.html
Copyright © 2011-2022 走看看