swift - 如何访问枚举中第二部分的第一项?

标签 swift

我在菜单中制作了一张包含 2 个部分的幻灯片,并将所有案例写在一个枚举中。 我知道每个部分都从索引 0 开始,并且我会为不同部分中的每个项目获得相同的值。

enum MenuType: Int {
//section 1
//section 2 }

class MenuViewController: UITableViewController {

@IBOutlet weak var imageView: UIImageView!
var didTapMenuType: ((MenuType) -> Void)?

override func viewDidLoad() {
    super.viewDidLoad()
    setupImageView()
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard let menuType = MenuType(rawValue: indexPath.row) else { return }
    dismiss(animated: true) { [weak self] in
        self?.didTapMenuType?(menuType)
    }
}

private func setupImageView() {
    imageView.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
}

是否可以编辑我的代码,使我的案例 .sync 具有与我案例中的 .plan 不同的值?

最佳答案

如下向枚举添加节和行属性

enum MenuType: Int, CaseIterable, CustomStringConvertible {

    //section 1
    case plan, documentation, visitlist, document, constructdiary, plancorrection
    //section 2
    case sync, settings, info

    var section: Int {
        switch self {
        case .plan,.documentation,.visitlist,.document,.constructdiary,.plancorrection: return 0
        case .sync,.settings,.info: return 1
        }
    }
    var row: Int? {
        switch self.section {
        case 0: return self.rawValue
        case 1: return self.rawValue - MenuType.allCases.filter { $0.section < self.section }.count
        default: return nil
        }
    }
    var description: String {
        switch self {
        case .plan: return "plan"
        case .documentation: return "documentation"
        case .visitlist: return "visitlist"
        case .document: return "document"
        case .constructdiary: return "constructdiary"
        case .plancorrection: return "plancorrection"
        case .sync: return "sync"
        case .settings: return "settings"
        case .info: return "info"
        }
    }
}

通过比较indexPath部分和行可以得到选中的菜单类型

class MenuViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return Array(Set(MenuType.allCases.map { $0.section })).count
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return MenuType.allCases.filter{ $0.section == section }.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")//tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let type = MenuType.allCases.first(where: { $0.section == indexPath.section && $0.row == indexPath.row })
        cell.textLabel?.text = type?.description
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedMenuType = MenuType.allCases.first(where: { $0.section == indexPath.section && $0.row == indexPath.row })
    }
}

关于swift - 如何访问枚举中第二部分的第一项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56495337/

相关文章:

ios - 从 TabViewController 中的 NavigationController 返回上一个 ViewController

ios - Swift MapView -> 以编程方式启动 "didSelectAnnotation"与用手指点击注释不同

ios - UIDocumentInteractionController 操作/选项菜单在首次启动时不可见

ios - Swift CocoaPod 可点击但 Xcode 找不到

swift - 在 swift 中捕获闭包的值是否意味着创建静态变量?

Swift 字典默认值

swift - 如何让 Sprite 在圆圈内遵循随机模式?

swift - 如何添加封面图片

ios - 自定义 UISlider 不会结束

swift - 无法从 iOS 应用程序打开设置 URL 字符串