美文网首页
Swift可选协议方法, 这一篇就够了!

Swift可选协议方法, 这一篇就够了!

作者: 书包里的码农 | 来源:发表于2019-06-30 12:24 被阅读0次

引言: 使用swift的小伙伴肯定被如何实现一个可选协议方法而困扰过, 这里为大家提供一个自定义UITableViewCell的实例, 相信能够帮到你.

ZZTableViewCell中的实现

import UIKit

//以前必须写为@objc protocol ZZTableViewCellDelegate : NSObjectProtocol的形式, 现在直接这么写就行了
@objc protocol ZZTableViewCellDelegate {
    
    //必选协议方法
    func ZZTableViewCellRequiredFunction()
    
    //可选协议方法
    @objc optional
    func ZZTableViewCellOptionalFunction()
    
}

class ZZTableViewCell: UITableViewCell {
    
    var delegate : ZZTableViewCellDelegate?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = UITableViewCell.SelectionStyle.none
        
        let button = UIButton.init()
        contentView.addSubview(button)
        button.frame = contentView.bounds
        button.addTarget(self, action:#selector(buttonClick(button:)) , for: UIControl.Event.touchUpInside)
        
    }
    
    @objc func buttonClick(button: UIButton) {
        
        //调用必选协议方法
        delegate?.ZZTableViewCellRequiredFunction()
        
        //调用可选协议方法
        delegate?.ZZTableViewCellOptionalFunction?()
        
//        以前的形式(打开会报错, 因为delegate没有继承与NSObjectProtocol)
//        if delegate?.responds(to: #selector(ZZTableViewCellDelegate.ZZTableViewCellOptionalFunction)) {
//            delegate?.ZZTableViewCellOptionalFunction?()
//        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

控制器中的使用

//MARKl- tableView
extension ZZHomeVC: UITableViewDelegate, UITableViewDataSource, ZZTableViewCellDelegate {
    
    func createTableView() {
        
        tableView = UITableView.init(frame: self.view.bounds, style: UITableView.Style.plain)
        
        view.addSubview(tableView)
        
        tableView.delegate = self
        
        tableView.dataSource = self;
        
        tableView.register(ZZTableViewCell.classForCoder(), forCellReuseIdentifier: "ZZTableViewCell")
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ZZTableViewCell", for: indexPath) as! ZZTableViewCell
        
        cell.textLabel?.text = "这里是第: " + String(indexPath.row) + "行, 点击试试看"
        
        cell.delegate = self
        
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }
    
    //注意, 这里只实现了必选的协议方法哦
    func ZZTableViewCellRequiredFunction() {
        print("必选的协议方法被调用了")
    }
    
//    //注意, 这里是可选协议方法, 不实现也不会报错
//    func ZZTableViewCellOptionalFunction() {
//        print("可选的协议方法被调用了")
//    }
    
}
关于我们:

有用的话, 记得留下你的小星星, 让更多人知道哦.
欢迎吐槽, qq邮箱: 1156858877@qq.com

相关文章

网友评论

      本文标题:Swift可选协议方法, 这一篇就够了!

      本文链接:https://www.haomeiwen.com/subject/kozvcctx.html