为什么自定义UITableViewCell点击事件会自动响应

时间:2016-04-24 14:23:11

标签: ios swift

正如标题所描述的,当初始化tableview时,单元格的单击事件将自动响应。 我创建了一个xib tableViewCell和swift文件,我只是做了一些简单的值赋值和布局约束

var model:SFmodel?{
    didSet{
        cardIdLable.text = model!.cardId
        nameLable.text = model!.name
        avtarImg.layer.cornerRadius = avtarImg.size.width / 2
        avtarImg.layer.masksToBounds = true
        avtarImg.sd_setImageWithURL(NSURL(string: model!.iconName))
    }  
}
override func awakeFromNib() {

    super.awakeFromNib()
    // Initialization code
    let contentView = self.contentView
    contentView.backgroundColor = UIColor.whiteColor()
    avtarImg.frame = CGRectMake(0, 0, 40, 40)
    avtarImg.sd_layout().topSpaceToView(contentView,5).leftSpaceToView(contentView,5).heightIs(40).widthIs(40)       nameLable.sd_layout().topSpaceToView(contentView,5).leftSpaceToView(avtarImg,5).widthIs(100).heightIs(20)     cardIdLable.sd_layout().topSpaceToView(nameLable,2).leftSpaceToView(avtarImg,8).widthIs(100).heightIs(10)
    nameLable.setSingleLineAutoResizeWithMaxWidth(180)
    cardIdLable.setSingleLineAutoResizeWithMaxWidth(180)
}
override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    print("has response")
}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

当未选择单元格时,也会调用setSelected。如果你只想在单元格被选中时被调用,你需要检查所选的bool,如:

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    if selected {
        print("has response")
    }
}