• 自定义带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 !
  • 相关阅读:
    5种排序算法
    Numpy 基础
    Git 帮助
    SpringBoot巧用 @Async 提升API接口并发能力
    延时队列实现的方式总结
    Spring Boot 进行优雅的字段校验
    分布式搜索引擎Elasticsearch的架构分析
    Redis 使用规范
    Intellij IDEA远程debug线上项目记录
    领域驱动设计:领域接口化设计
  • 原文地址:https://www.cnblogs.com/mogul/p/5157699.html
Copyright © 2020-2023  润新知