单选按钮在 CollectionView 单元格中不工作,如果我 select 一个按钮其他按钮不 Deselecting
Radio Button Not Working In ColectionView Cell , If I select One Button another Buttons aren't Deselecting
这是我的图像结构,是否选择了图像。
struct TeamSelected {
var logoImage: String
var isImageSelected: Bool }
这是检查选择的变量
var selection = Set<Int>()
我在索引路径方法中的行单元格...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let teamSelection : TeamSelectionCollectionViewCell = self.teamCollectionView.dequeueReusableCell(withReuseIdentifier: "teamCell", for: indexPath) as! TeamSelectionCollectionViewCell
let index = indexPath.row
teamSelection.logoImage.image = UIImage(named: teamSelectionList[index].logoImage)
let isImageSelected = selection.contains(index)
teamSelection.logoButton.isSelected = isImageSelected
teamSelection.logoButton.setImage(
UIImage(named: isImageSelected ? "ic_radio_selected" : "ic_radio_normal"),
for: UIControl.State.normal
)
teamSelection.logoButton.tag = indexPath.row
teamSelection.logoButton.addTarget(self, action: #selector(logoButtonTapped), for: .touchUpInside)
teamSelection.seperatorView.isHidden = indexPath.row == 2 || indexPath.row == self.teamSelectionList.count - 1 ? true : false
return teamSelection
}
这是按钮目标函数...
@objc func logoButtonTapped(sender: UIButton){
let index = sender.tag
if (selection.contains(index)){
selection.remove(index)
} else {
selection.insert(index)
}
self.teamCollectionView.reloadData()
}[![Here's My simulator Image, As You Can See if select A button another button is not Deselcting.][1]][1]
如果你只想选择一个,你基本上可以这样做:
@objc func logoButtonTapped(sender: UIButton){
let index = sender.tag
selection.removeAll()
selection.append(index)
self.teamCollectionView.reloadData()
}
此条件 (selection.contains(index))
仅在索引是 selection
数组的一部分时有效,因此当您添加新索引时,旧索引不会删除
struct TeamSelected {
var logoImage: String
var isImageSelected: Bool }
这是检查选择的变量
var selection = Set<Int>()
我在索引路径方法中的行单元格...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let teamSelection : TeamSelectionCollectionViewCell = self.teamCollectionView.dequeueReusableCell(withReuseIdentifier: "teamCell", for: indexPath) as! TeamSelectionCollectionViewCell
let index = indexPath.row
teamSelection.logoImage.image = UIImage(named: teamSelectionList[index].logoImage)
let isImageSelected = selection.contains(index)
teamSelection.logoButton.isSelected = isImageSelected
teamSelection.logoButton.setImage(
UIImage(named: isImageSelected ? "ic_radio_selected" : "ic_radio_normal"),
for: UIControl.State.normal
)
teamSelection.logoButton.tag = indexPath.row
teamSelection.logoButton.addTarget(self, action: #selector(logoButtonTapped), for: .touchUpInside)
teamSelection.seperatorView.isHidden = indexPath.row == 2 || indexPath.row == self.teamSelectionList.count - 1 ? true : false
return teamSelection
}
这是按钮目标函数...
@objc func logoButtonTapped(sender: UIButton){
let index = sender.tag
if (selection.contains(index)){
selection.remove(index)
} else {
selection.insert(index)
}
self.teamCollectionView.reloadData()
}[![Here's My simulator Image, As You Can See if select A button another button is not Deselcting.][1]][1]
如果你只想选择一个,你基本上可以这样做:
@objc func logoButtonTapped(sender: UIButton){
let index = sender.tag
selection.removeAll()
selection.append(index)
self.teamCollectionView.reloadData()
}
此条件 (selection.contains(index))
仅在索引是 selection
数组的一部分时有效,因此当您添加新索引时,旧索引不会删除