相同的代码在一个项目中起作用,而在另一个项目中不起作用

时间:2017-11-08 19:37:30

标签: ios swift uitableview

我正在处理一个具有静态表视图的项目。此表视图有三个部分,每个部分都有不同的行数。我使用的代码允许同时在不同的部分选择不同的行,但不能从同一部分中选择两个不同的行。这是代码:

switch indexPath.section {
    case 0:
        switch indexPath.row {
        case 0:
            oneDollarRestaurantCell.accessoryType = .checkmark
            oneDollarRestaurantCell.isSelected = true
            twoDollarRestaurantCell.accessoryType = .none
            twoDollarRestaurantCell.isSelected = false
            threeDollarRestaurantCell.accessoryType = .none
            threeDollarRestaurantCell.isSelected = false
            fourDollarRestaurantCell.accessoryType = .none
            fourDollarRestaurantCell.isSelected = false
        case 1:
            twoDollarRestaurantCell.accessoryType = .checkmark
            twoDollarRestaurantCell.isSelected = true
            oneDollarRestaurantCell.accessoryType = .none
            oneDollarRestaurantCell.isSelected = false
            threeDollarRestaurantCell.accessoryType = .none
            threeDollarRestaurantCell.isSelected = false
            fourDollarRestaurantCell.accessoryType = .none
            fourDollarRestaurantCell.isSelected = false
        case 2:
            threeDollarRestaurantCell.accessoryType = .checkmark
            threeDollarRestaurantCell.isSelected = true
            twoDollarRestaurantCell.accessoryType = .none
            twoDollarRestaurantCell.isSelected = false
            oneDollarRestaurantCell.accessoryType = .none
            oneDollarRestaurantCell.isSelected = false
            fourDollarRestaurantCell.accessoryType = .none
            fourDollarRestaurantCell.isSelected = false
        case 3:
            fourDollarRestaurantCell.accessoryType = .checkmark
            fourDollarRestaurantCell.isSelected = true
            twoDollarRestaurantCell.accessoryType = .none
            twoDollarRestaurantCell.isSelected = false
            threeDollarRestaurantCell.accessoryType = .none
            threeDollarRestaurantCell.isSelected = false
            oneDollarRestaurantCell.accessoryType = .none
            oneDollarRestaurantCell.isSelected = false
        default:
            break

        }
    case 1:
        switch indexPath.row {
        case 0:
            within500MetersCell.accessoryType = .checkmark
            within500MetersCell.isSelected = true
            within1000MetersCell.accessoryType = .none
            within1000MetersCell.isSelected = false


        case 1:
            within1000MetersCell.accessoryType = .checkmark
            within1000MetersCell.isSelected = true
            within500MetersCell.accessoryType = .none
            within500MetersCell.isSelected = false

        default:
            break
        }
    case 2:
        switch indexPath.row {
        case 0:
            sortByFromAtoZCell.accessoryType = .checkmark
            sortByFromAtoZCell.isSelected = true
            sortByFromZtoACell.accessoryType = .none
            sortByFromZtoACell.isSelected = false
            sortByDistanceCell.accessoryType = .none
            sortByDistanceCell.isSelected = false
            sortByPriceCell.accessoryType = .none
            sortByPriceCell.isSelected = false
        case 1:
            sortByFromZtoACell.accessoryType = .checkmark
            sortByFromZtoACell.isSelected = true
            sortByFromAtoZCell.accessoryType = .none
            sortByFromAtoZCell.isSelected = false
            sortByDistanceCell.accessoryType = .none
            sortByDistanceCell.isSelected = false
            sortByPriceCell.accessoryType = .none
            sortByPriceCell.isSelected = false
        case 2:
            sortByDistanceCell.accessoryType = .checkmark
            sortByDistanceCell.isSelected = true
            sortByFromZtoACell.accessoryType = .none
            sortByFromZtoACell.isSelected = false
            sortByFromAtoZCell.accessoryType = .none
            sortByFromAtoZCell.isSelected = false
            sortByPriceCell.accessoryType = .none
            sortByPriceCell.isSelected = false
        case 3:
            sortByPriceCell.accessoryType = .checkmark
            sortByPriceCell.isSelected = true
            sortByFromZtoACell.accessoryType = .none
            sortByFromZtoACell.isSelected = false
            sortByDistanceCell.accessoryType = .none
            sortByDistanceCell.isSelected = false
            sortByFromAtoZCell.accessoryType = .none
            sortByFromAtoZCell.isSelected = false

        default:
            break
        }

    default:
        break
    }

}

在这里,我检查各部分,在其中,我检查每一行。这工作正常,但突然停止了。

已编辑

预期结果如下:

enter image description here

实际结果如下:

enter image description here

我没有改变那堂课内的任何东西。相同的代码仍然适用于另一个项目,因此我非常绝望,因为我无法理解可能导致问题的原因。我已经尝试了一切,甚至将文件从工作项目复制到这个项目(文件结构完全相同)。你有什么建议吗?

2 个答案:

答案 0 :(得分:2)

据我所知,您提供的示例之间的主要区别在于多重选择。要为UITableView启用此功能,您可以在属性检查器中将selection属性的值设置为multiple selection

您还可以通过将allowsMultipleSelection属性设置为true来覆盖此参数。我不完全确定,但我怀疑在你的第一个项目中,这个属性是用代码设置的。

答案 1 :(得分:1)

主要区别在于表格的色调可以从任何方式改变!默认为蓝色,您已将其意外更改为白色或更改应用中的其他内容。只需将其重新设置为蓝色。