我有一个xib视图,其中我带了一个tableView
customcell xib。在这个自定义单元格中,我有一个复选框按钮,其行为类似于使用自定义单元格进行检查和取消选中。但是,当我点击第一个单元格复选框作为第9个单元格的第9行单元格,第18行单元格的倍数时,......也变成了勾号。同时滚动复选框刻度选项在单元格之间变化。我无法知道这是为什么发生.. ??为什么我在自定义单元格中的复选框在swift中选择和滚动时显示不同的行为?
我已经注册了电池厦门国际银行视图:
override func viewDidLoad() {
super.viewDidLoad()
//Register custom cell
let nib = UINib(nibName: "CustomOneCell", bundle: nil)
AddOnTableView.registerNib(nib, forCellReuseIdentifier: "addoncell")
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ADDONITEMS.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CustomOneCell = AddOnTableView.dequeueReusableCellWithIdentifier("addoncell") as! CustomOneCell
let item: AddOnItems = ADDONITEMS[indexPath.row]
cell.addOnName.text = item.name
cell.addOnPrice.text = "£\(item.price!)"
return cell
}
对于复选框,我加入了如下自定义类:
var isCheckedAddOnGlobal = Bool()
class AddOnCheckBox: UIButton {
let checkedImage = UIImage(named: "checkboxredtick.png")! as UIImage
let unCheckedImage = UIImage(named:"checkbox untick.png")!as UIImage
//bool property
var ischecked:Bool = false{
didSet{
//print(ischecked)
if ischecked == true{
self.setImage(checkedImage, forState: .Normal)
}else{
self.setImage(unCheckedImage, forState: .Normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action:#selector(CheckBox.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.ischecked = false
}
func buttonClicked(sender: UIButton) {
if (sender == self) {
if ischecked == true{
ischecked = false
isCheckedAddOnGlobal = false
}else{
ischecked = true
isCheckedAddOnGlobal = true
}
}
}
}
你为什么不使用didSelectCell方法,而不是添加怪异的目标,只是用buttonClicked在didSelectCell方法,这将有助于 –
我的按钮是在自定义单元格,所以我认为我没有添加目标我怎么能设置任何操作按钮单击,如果单元格内容在另一个超文本类型的UITableViewCell @ Lu_ –
swift文件中检查我的答案,不要使用单元格作为按钮,它有自己的方法单击和其他动作 –