你好,我有一个集合视图,其中我在每个单元格中都有一个复选框。我使用此复选框https://github.com/zhouhao27/WOWCheckbox。
所有单元格都有自己的复选框,但正如标题所说,问题是当我点击复选框时,所有的复选框都会被选中。实际上,当我点击第一个所有赔率复选框(1-3-5-7-...)被选中,当我点击第二个按钮时,所有的复选框都被选中。
我将视图连接到我的单元文件,并将其更改为WOWCheckbox,如文档所述。
我没有改变任何其他的东西。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Interest Cell", forIndexPath: indexPath) as! ThirdTabCell
cell.check1.tag = indexPath.row
cell.check1.addTarget(self, action: #selector(ThirdTab.follow(_:)), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func follow(sender:WOWCheckbox!) {
print("check")
}
当我使用此代码时,当我点击复选框时,它只打印复选一次。我想我必须声明我点击的是哪张支票,但我不知道如何使用它。
发布于 2016-06-29 20:42:40
// func follow(sender:WOWCheckbox!) {
// sender.tag//will get the which row you checked
//do logic based on tag
//}
//创建模型
class checkList{
var item = ""
var checked = false
}
//Controller
class SampleViewController: ViewController,WOWCheckboxDelegate{
var List = [checkList]()
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...10 {
let object = checkList()
object.item = i.description
object.checked = false
List.append = obj
}
_collectionview.reloadData()
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Interest Cell", forIndexPath: indexPath) as! ThirdTabCell
cell.check1.tag = indexPath.row
if List[indexPath.row].checked {
cell.check1.isChecked = true
}
else{
cell.check1.isChecked = false
}
return cell
}
//delegate function by WOWCheckbox
func didSelectCheckbox(checkbox : WOWCheckbox) {
if checkBox.tag == 0{
//first row
for i ...< List.count {
if i % 2 != 0 {
List[i].checked = true
}
else{
List[i].checked = false
}
collectionView.reloadData()
return
}
}
//second row
else if checkBox.tag == 1{
for obj in List {
obj.checked = true
}
collectionView.reloadData()
return
}
else {
if list[checkbox.tag].isChecked {
checkBox.isChecked = false
list[checkbox.tag].checked = false
}
else{
list[checkbox.tag].checked = true
checkBox.isChecked = true
}
}
}
}
我没有测试,请检查代码并根据您的要求进行更改
https://stackoverflow.com/questions/38099692
复制相似问题