Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:UICollectionView中的单选按钮_Ios_Swift_Uicollectionview_Radio Button - Fatal编程技术网

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。。