gpt4 book ai didi

ios - TableView 不符合协议(protocol)

转载 作者:可可西里 更新时间:2023-11-01 00:38:45 25 4
gpt4 key购买 nike

我已经设置了一个基本的 CellAssociation 协议(protocol)。

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

"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
}
}

这是一个试图遵守协议(protocol)的 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
}
}

最佳答案

你的协议(protocol)有这样的要求:

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

但是您的 TableView 子类 LineupDraftSortMenuTableView 从未实现该方法。所以不符合协议(protocol)。

也许您假设该函数声明与 UITableView 已经实现的内容相匹配,因此 UITableView 子类可以符合您的协议(protocol)而无需显式实现它。但事实并非如此。 UITableView 已有的方法是:

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

下划线有很大的不同!

因此,如果您重写协议(protocol)及其扩展以匹配 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 {
// ...
}

关于ios - TableView 不符合协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53128953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com