【发布时间】:2018-11-03 06:22:15
【问题描述】:
我已经建立了一个基本的 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
}
}
【问题讨论】:
-
什么是
LineupDraftSortMenuCell? -
我在帖子中添加了类定义,希望对您有所帮助!
-
检查答案:D
标签: ios swift uitableview uikit