无法将类型 'FriendTableViewCell.Type' 的值转换为预期的参数类型 'FriendTableViewCell'
Cannot convert value of type 'FriendTableViewCell.Type' to expected argument type 'FriendTableViewCell'
所以我正在尝试为我的应用程序设置好友列表,但我 运行 遇到一个错误,我被告知编译无法将类型 'FriendTableViewCell.Type' 的值转换为预期值参数类型 'FriendTableViewCell'。这让我感到困惑,因为它看起来是一样的。也许我遗漏了什么?
我遇到问题的代码是:
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: FriendTableViewCell, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: FriendTableViewCell, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
我的完整代码是:
import Foundation
protocol FriendTableViewCellDelegate: class{
func cell(cell: FriendTableViewCell, didSelectFollowUser user: PFUser)
func cell(cell: FriendTableViewCell, didSelectUnfollowUser user: PFUser)
}
class FriendTableViewCell: UITableViewCell{
@IBOutlet weak var friendName: UILabel!
@IBOutlet weak var followButton: UIButton!
weak var delegate: FriendTableViewCellDelegate?
var user: PFUser? {
didSet {
friendName.text = user?.username
}
}
var canFollow: Bool? = true {
didSet {
if let canFollow = canFollow {
followButton.isSelected = !canFollow
}
}
}
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: FriendTableViewCell, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: FriendTableViewCell, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
}
错误提示您需要提供 FriendTableViewCell
类型的对象,而不是 FriendTableViewCell
类型本身。
只需在您的函数中将 FriendTableViewCell
替换为 self
:
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: self, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: self, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
我想你只是想说 self
它将传递特定的单元格,而不是仅仅传递通用类型。
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: self, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: self, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
正如其他张贴者所说,您的委托函数期望您传入一个单元格,但您在调用中传入了单元格的 CLASS。
在单元格中传递是个坏主意。您可能应该重构代码以传入所选单元格的 indexPath,而不是单元格本身。
所以我正在尝试为我的应用程序设置好友列表,但我 运行 遇到一个错误,我被告知编译无法将类型 'FriendTableViewCell.Type' 的值转换为预期值参数类型 'FriendTableViewCell'。这让我感到困惑,因为它看起来是一样的。也许我遗漏了什么?
我遇到问题的代码是:
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: FriendTableViewCell, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: FriendTableViewCell, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
我的完整代码是:
import Foundation
protocol FriendTableViewCellDelegate: class{
func cell(cell: FriendTableViewCell, didSelectFollowUser user: PFUser)
func cell(cell: FriendTableViewCell, didSelectUnfollowUser user: PFUser)
}
class FriendTableViewCell: UITableViewCell{
@IBOutlet weak var friendName: UILabel!
@IBOutlet weak var followButton: UIButton!
weak var delegate: FriendTableViewCellDelegate?
var user: PFUser? {
didSet {
friendName.text = user?.username
}
}
var canFollow: Bool? = true {
didSet {
if let canFollow = canFollow {
followButton.isSelected = !canFollow
}
}
}
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: FriendTableViewCell, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: FriendTableViewCell, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
}
错误提示您需要提供 FriendTableViewCell
类型的对象,而不是 FriendTableViewCell
类型本身。
只需在您的函数中将 FriendTableViewCell
替换为 self
:
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: self, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: self, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
我想你只是想说 self
它将传递特定的单元格,而不是仅仅传递通用类型。
@IBAction func followButtonTap(_ sender: Any) {
if let canFollow = canFollow, canFollow == true {
delegate?.cell(cell: self, didSelectFollowUser: PFUser)
self.canFollow = false
} else {
delegate?.cell(cell: self, didSelectUnfollowUser: PFUser)
self.canFollow = true
}
}
正如其他张贴者所说,您的委托函数期望您传入一个单元格,但您在调用中传入了单元格的 CLASS。
在单元格中传递是个坏主意。您可能应该重构代码以传入所选单元格的 indexPath,而不是单元格本身。