将自定义 UIView 添加到 TableViewCell
Posted
技术标签:
【中文标题】将自定义 UIView 添加到 TableViewCell【英文标题】:Adding Custom UIView to TableViewCell 【发布时间】:2017-06-24 01:25:01 【问题描述】:我未能成功地将我的自定义 UIView 添加到我的 TableViewCell。此自定义视图代表每个表格视图单元格的自定义复选标记。我在没有故事板的情况下做我的项目,我不确定如何以编程方式将此自定义 UIView 添加到我的 UITableViewCell 中。这是我的代码:
class CheckBoxView: UIView
var isChecked: Bool
var checkBoxImageView: UIImageView
var checkBoxChanged :() -> () =
required init?(coder aDecoder: NSCoder)
self.isChecked = false
self.checkBoxImageView = UIImageView(image: nil)
super.init(coder: aDecoder)
setup()
func setup()
self.layer.borderWidth = 1.0
self.isUserInteractionEnabled = true
self.checkBoxImageView.frame = CGRect(x: 2, y: 2, width: 25, height: 25)
self.addSubview(self.checkBoxImageView)
let selector: Selector = "checkBoxTapped"
let tapRecognizer = UITapGestureRecognizer(target: self, action: selector)
self.addGestureRecognizer(tapRecognizer)
func checkBoxTapped()
self.checkBoxChanged()
func markAsChecked()
self.checkBoxImageView.image = UIImage(named: "new_message_icon")
func markAsUnChecked()
self.checkBoxImageView.image = nil
这是我的 TableViewCell:
class AddContactsCell: UITableViewCell
let profileImageView: UIImageView =
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.layer.cornerRadius = 20
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
return imageView
()
let checkBox: CheckBoxView =
let view = UIView() as! CheckBoxView
view.backgroundColor = UIColor.blue
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 16
view.layer.masksToBounds = true
return view
()
override func layoutSubviews()
super.layoutSubviews()
textLabel?.frame = CGRect(x: 100, y: textLabel!.frame.origin.y - 2, width: textLabel!.frame.width, height: textLabel!.frame.height)
detailTextLabel?.frame = CGRect(x: 101, y: detailTextLabel!.frame.origin.y + 1, width: detailTextLabel!.frame.width, height: detailTextLabel!.frame.height)
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
addSubview(profileImageView)
self.addSubview(checkBox)
//ios 9 constraint anchors
//need x,y,width,height anchors
profileImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 50).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
checkBox.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10).isActive = true
checkBox.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
checkBox.widthAnchor.constraint(equalToConstant: 30).isActive = true
checkBox.heightAnchor.constraint(equalToConstant: 30).isActive = true
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
如果您能告诉我添加 UIView 出了什么问题,我将不胜感激。
【问题讨论】:
你的cellForRow
方法在哪里?
方明宁在我的桌子上但是我没有贴,因为代码很多
好的。所以你的图像视图是正确添加的,但不是你的自定义视图,对吧?
完全正确。我的自定义视图出现此错误:无法将“UIView”(0x107439f40)类型的值转换为“chatApplication.CheckBoxView”
【参考方案1】:
您应该实例化您的 CheckBoxView
而不是将普通的 UIView
投射到您的 CheckBoxView
改变这一行:
let view = UIView() as! CheckBoxView
到这一行
let view = CheckBoxView()
【讨论】:
它要求 let view = CheckBoxView(coder: ) 但我不确定在编码器中放什么。我使用了一个教程来设置它,所以我不是 100% 熟悉代码是如何工作的 然后你添加一个init
也没有你的CheckBoxView
编码器
我对此有点陌生。你能举一个代码的例子吗?我真的很感激
请使用 Stackoverview/Google 搜索! ***.com/questions/27056864/swift-subclass-uiview
关键字:子类 uiview以上是关于将自定义 UIView 添加到 TableViewCell的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式将自定义 UIView 添加到 UIScrollView 之上的 Stackview?
如何以编程方式将自定义 uiview 添加到视图控制器 SWIFT