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 !
  • 相关阅读:
    npm, node, pm2 使用笔记
    没加证书的域名通过https访问,错误的访问到有证书的域名项目--已解决
    mysql数据库大表加索引
    上传大文件失败
    ifame 与父页面进行数据交互(跨域)
    windows平台编译PHP及扩展 和 踩过的坑
    vim 使用笔记
    git 在pull/push指定密钥文件
    记一次使用Xshell登陆提示所选用户密钥未在远程主机上注册
    学习网站与参考文档
  • 原文地址:https://www.cnblogs.com/mogul/p/5157699.html
Copyright © 2011-2022 走看看