Ios Swift:UICollectionView中的单选按钮
我正在使用Ios Swift:UICollectionView中的单选按钮,ios,swift,uicollectionview,radio-button,Ios,Swift,Uicollectionview,Radio Button,我正在使用UICollectionView创建应用程序设置。我知道,UITableView更适合这样的静态对象,但我需要集合视图的可定制性 我有2个部分,每个部分有2个按钮。作为按钮,我使用自定义XIB,对于选中/未选中,我使用其功能。我用这段代码实现了它,但我觉得这不是一段好代码——如果你知道我的意思的话 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: Ind
UICollectionView
创建应用程序设置。我知道,UITableView
更适合这样的静态对象,但我需要集合视图的可定制性
我有2个部分,每个部分有2个按钮。作为按钮,我使用自定义XIB,对于选中/未选中,我使用其功能。我用这段代码实现了它,但我觉得这不是一段好代码——如果你知道我的意思的话
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.section == 0 {
let cell00 = collectionView.cellForItem(at: [0,0]) as! RadioButtonCell
let cell01 = collectionView.cellForItem(at: [0,1]) as! RadioButtonCell
if indexPath.row == 0 {
cell00.selectCell()
cell01.unselectCell()
} else {
cell00.unselectCell()
cell01.selectCell()
}
} else {
let cell10 = collectionView.cellForItem(at: [1,0]) as! RadioButtonCell
let cell11 = collectionView.cellForItem(at: [1,1]) as! RadioButtonCell
if indexPath.row == 0 {
cell10.selectCell()
cell11.unselectCell()
} else {
cell10.unselectCell()
cell11.selectCell()
}
}
}
能做得更好吗?谢谢重写func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
for i in 0..<collectionView.numberOfItems(inSection: indexPath.section) {
let cell = collectionView.cellForItem(at: [indexPath.section, i]) as! RadioButtonCell
i == indexPath.row ? cell.selectCell() : cell.unselectCell()
}
}
因为我在0。。