首页 > 解决方案 > Swift:TableView didSelectRowAt 仅针对某些行触发?

问题描述

我目前正在向我的设置表视图添加功能,并且由于某种原因,表视图 didSelectRowAt 函数仅在我选择每个部分的第一行时才会触发。

我使用枚举设置了表格视图,但我不明白为什么会导致问题。

这是我的 didSelectRowAtFunction:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let section = SettingsSections(rawValue: indexPath.section) else { return }
        guard let accountSection = AccountOptions(rawValue: indexPath.row) else { return }
        guard let financialSection = PaymentOptions(rawValue: indexPath.row) else { return }
        guard let communicationsSection = CommunicationsOptions(rawValue: indexPath.row) else { return }
        switch section {
        case .Account:
            switch accountSection {
                case .editAccountInfor:
                    print("Edit account pressed")
                case .logout:
                    print("logout pressed")
            }
        case .Communications:
            switch communicationsSection {
                case .notifacations:
                    print("notifs pressed")
            }
        case .Financial:
            switch financialSection {
                case .linkBankAccount:
                    print("LBA pressed")
                case .viewPaymentOptions:
                    print("payment options pressed")
            }
        }
    }

当我按下编辑账户信息行、链接银行账户行或通知行时,它完全可以触发。但是,当我按下该部分中不是第一行的任何行时,它不会显示任何语句。

以防万一,这是我的 SettingsSections 代码以及 AccountOptions 让你们看看我是如何设置的。

enum SettingsSections: Int, CaseIterable, SectionType {
    case Account
    case Financial
    case Communications

    var containsSwitch: Bool {
        return false
    }
    
    var description: String {
        switch self {
        case .Account: return "Account"
        case .Financial: return "Financial"
        case .Communications: return "Communications"
        }
    }
}

enum AccountOptions: Int, CaseIterable, SectionType {
    case editAccountInfor
    case logout
    
    var containsSwitch: Bool {
        return false
    }
    var description: String {
        switch self {
        case .editAccountInfor: return "Edit Account Information"
        case .logout: return "Log Out"
        }
    }
}

标签: swift

解决方案


推荐阅读