TableView不符合协议

时间:2018-11-03 06:22:15

标签: ios swift uitableview uikit

我已经建立了基本的CellAssociation协议。

但是,我添加到协议中的任何内容都会得到:

"Type 'FooTableView' does not conform to protocol 'Cell Association'"

Xcode似乎给了我一些提示:

"Multiple maching functions named 'register(cellClass:forCellReuseIdentifier:)' with type '(AnyClass?, String) -> ()' (aka '(Optional<AnyObject.Type>, String) -> ()')"

和..

"Rename to 'register(cellClass:forCellReuseIdentifier:)' to satisfy this requirement"

但是,看来我的注册函数就是这样命名的。

这是CellAssociation(TableView.swift)

import UIKit

protocol CellAssociation {
    associatedtype Cell: UITableViewCell

    func register()
    func register(cellClass: AnyClass?, forCellReuseIdentifier: String)

    func dequeueReusableCell(for: IndexPath) -> Cell
    func dequeueReusableCell(withIdentifier: String, for: IndexPath) -> UITableViewCell
}

extension CellAssociation {

    func register() {
        register(cellClass: Cell.self, forCellReuseIdentifier: String(describing: Cell.self))
    }

    func dequeueReusableCell(for indexPath: IndexPath) -> Cell {
        return dequeueReusableCell(withIdentifier: String(describing: Cell.self), for: indexPath) as! Cell
    }
}

这是一个试图遵守该协议的TableView:

import UIKit

class LineupDraftSortMenuTableView: UITableView, CellAssociation {

    typealias Cell = LineupDraftSortMenuCell

    init() {
        super.init(frame: CGRect.zero, style: .plain)
        setup()
    }

    required convenience init?(coder: NSCoder) {
        self.init()
    }

    func setup() {
        rowHeight = 40
        separatorStyle = .none
        backgroundColor = UIColor.clear
        register()
    }
}

此类将引发错误:

"Type 'LineupDraftSortMenuTableView' does not conform to protocol 'CellAssociation'"

和LineupDraftSortMenuCell

import UIKit

class LineupDraftSortMenuCell: UITableViewCell {

    let optionLabel = DraftboardLabel()
    let iconCheck = UIImageView()

    let borderView = UIView()

    var selectedOption: Bool = false { didSet { toggleIconCheck() } }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setup()
    }

    required convenience init?(coder: NSCoder) {
        self.init()
    }

    func setup() {
        addSubviews()
        setupSubviews()
        addConstraints()
    }

    func addSubviews() {
        contentView.addSubview(optionLabel)
        contentView.addSubview(iconCheck)
        contentView.addSubview(borderView)
    }

    func setupSubviews() {
        backgroundColor = UIColor.clear
        contentView.backgroundColor = UIColor.clear
        selectionStyle = .none

        optionLabel.font = UIFont.openSans(weight: .Semibold, size: 9)
        optionLabel.textColor = UIColor.white
        optionLabel.letterSpacing = 0.5

        iconCheck.image = UIImage(named: "icon-check")
        iconCheck.contentMode = .scaleAspectFit
        iconCheck.isHidden = !selectedOption

        borderView.backgroundColor = UIColor(0x5c656f)
    }

    func addConstraints() {
        let viewConstraints: [NSLayoutConstraint] = [
            optionLabel.leftRancor.constraintEqualToRancor(rancor: contentView.leftRancor, constant: 20),
            optionLabel.centerYRancor.constraintEqualToRancor(rancor: contentView.centerYRancor),
            iconCheck.widthRancor.constraintEqualToConstant(constant: 12),
            iconCheck.heightRancor.constraintEqualToConstant(constant: 10),
            iconCheck.centerYRancor.constraintEqualToRancor(rancor: contentView.centerYRancor),
            iconCheck.rightRancor.constraintEqualToRancor(rancor: contentView.rightRancor, constant: -20),
            borderView.leftRancor.constraintEqualToRancor(rancor: contentView.leftRancor, constant: 10),
            borderView.rightRancor.constraintEqualToRancor(rancor: contentView.rightRancor, constant: -10),
            borderView.bottomRancor.constraintEqualToRancor(rancor: contentView.bottomRancor),
            borderView.heightRancor.constraintEqualToConstant(constant: 1),
        ]

        optionLabel.translatesAutoresizingMaskIntoConstraints = false
        iconCheck.translatesAutoresizingMaskIntoConstraints = false
        borderView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate(viewConstraints)
    }

    func toggleIconCheck() {
        iconCheck.isHidden = !selectedOption
    }
}

2 个答案:

答案 0 :(得分:2)

实际上,最近,我几乎遇到了您可以参考的问题(Swift: Conforming to protocols using default values)。

因此,请参考此内容,这是您的代码的修改版本。

protocol CellAssociation {
    associatedtype Cell: UITableViewCell

    func register(cellClass: AnyClass?, forCellReuseIdentifier: String)
    func dequeueReusableCell(withIdentifier: String, forIndexPath indexPath: IndexPath) -> UITableViewCell
}

extension CellAssociation {

    func register(cellClass: AnyClass? = Cell.self, forCellReuseIdentifier: String = String(describing: Cell.self)) {
        return register(cellClass: cellClass, forCellReuseIdentifier: forCellReuseIdentifier)
    }

    func dequeueReusableCell(withIdentifier: String = String(describing: Cell.self), forIndexPath indexPath: IndexPath) -> UITableViewCell {
        return dequeueReusableCell(withIdentifier: withIdentifier, forIndexPath: indexPath)
    }
}

我要做的是,我通过了默认值,因为您为此创建了一个不同的方法,并在extension中进行了确认。现在,它将确认protocol。让我知道,如果您还有其他疑问或问题。

答案 1 :(得分:2)

您的协议具有以下要求:

protocol CellAssociation {
    func register(cellClass: AnyClass?, forCellReuseIdentifier: String)
}

但是您的表视图子类LineupDraftSortMenuTableView永远不会实现该方法。因此它不符合协议。

也许您假设该函数声明与UITableView已经实现的某些东西匹配,所以UITableView子类可以符合您的协议而无需显式实现它。但是事实并非如此。 UITableView已经拥有的方法是:

func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)

下划线有很大的不同!

因此,如果您重写协议及其扩展名以匹配UITableView已实现的内容,则代码将编译,如下所示:

protocol CellAssociation {
    associatedtype Cell: UITableViewCell
    func register()
    func register(_ cellClass: AnyClass?, forCellReuseIdentifier: String)
    func dequeueReusableCell(for: IndexPath) -> Cell
    func dequeueReusableCell(withIdentifier: String, for: IndexPath) -> UITableViewCell
}

extension CellAssociation {
    func register() {
        register(Cell.self, forCellReuseIdentifier: String(describing: Cell.self))
    }
    func dequeueReusableCell(for indexPath: IndexPath) -> Cell {
        return dequeueReusableCell(withIdentifier: String(describing: Cell.self), for: indexPath) as! Cell
    }
}

一旦您说了这一点,就可以合法地说:

class LineupDraftSortMenuTableView: UITableView, CellAssociation {
    // ...
}