首页 > 解决方案 > 如何通过案例从枚举 tableView 推送到新视图

问题描述

代码新手。遵循教程后,我正在寻找一种从每个案例中导航到新视图控制器的方法。下面代码中看到的每个案例都在一个枚举文件中。我不确定需要什么代码才能从每个单元格(索引路径)推送到新视图。任何人都可以帮忙吗?

表格视图有效,我可以从侧面菜单中找到它,那里没有问题。它是子视图的一部分。我已经尝试过 switch 语句,但我不确定如何将枚举带入我的“ProfileViewController”范围,正如您在我的枚举文件中看到的那样,这是一个设置页面,用户可以在其中编辑他们的个人资料并调整他们的偏好。我感谢我能得到的所有帮助

这是我的快速文件:

import UIKit


private let reuseIdentifier = "SettingsCell"


class ProfileViewController: UIViewController {
        
        // MARK: - Properties
        
        var tableView: UITableView!
        var userInfoHeader: UserInfoHeader!
        
        // MARK: - Init

        override func viewDidLoad() {
            super.viewDidLoad()
            configureUI()
            view.backgroundColor = UIColor(named: "RED")
        }

        // MARK: - Helper Functions
        
        func configureTableView() {
            tableView = UITableView()
            tableView.delegate = self
            tableView.dataSource = self
            tableView.rowHeight = 60
            
            tableView.register(SettingsCell.self, forCellReuseIdentifier: reuseIdentifier)
            view.addSubview(tableView)
            tableView.frame = view.frame
            
            let frame = CGRect(x: 0, y: 88, width: view.frame.width, height: 100)
            userInfoHeader = UserInfoHeader(frame: frame)
            userInfoHeader.backgroundColor = UIColor(named: "RED")
            tableView.tableHeaderView = userInfoHeader
            tableView.tableFooterView = UIView()
        }
        
        func configureUI() {
            configureTableView()
            
            navigationController?.navigationBar.prefersLargeTitles = true
            navigationController?.navigationBar.isTranslucent = true
            navigationController?.navigationBar.barTintColor = UIColor(named: "RED")
            navigationItem.title = "Profile"
        }

    }

    extension ProfileViewController: UITableViewDelegate, UITableViewDataSource {
        
        func numberOfSections(in tableView: UITableView) -> Int {
            return SettingsSection.allCases.count
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            
            guard let section = SettingsSection(rawValue: section) else { return 0 }
            
            switch section {
            case.Social: return SocialOptions.allCases.count
            case.Communications: return CommunicationOptions.allCases.count

                
            }
            
        }
        
        
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let view = UIView()
            view.backgroundColor = UIColor(named: "RED")
            
            
            let title = UILabel()
            title.font = UIFont.boldSystemFont(ofSize: 18)
            title.textColor = .yellow
            title.text = SettingsSection(rawValue: section)?.description
            view.addSubview(title)
            title.translatesAutoresizingMaskIntoConstraints = false
            title.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
            title.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 15).isActive = true
            
            return view
            
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 50
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! SettingsCell
            guard let section = SettingsSection(rawValue: indexPath.section) else { return UITableViewCell() }
            
            switch section {
            case.Social:
                let social = SocialOptions(rawValue: indexPath.row)
                cell.textLabel?.text = social?.description
            case.Communications:
                let communications = CommunicationOptions(rawValue: indexPath.row)
                cell.textLabel?.text = communications?.description
            }
            
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
        
                    }
            
        }

这是我的枚举文件:

enum SettingsSection: Int, CaseIterable, CustomStringConvertible {
    
    case Social 
    case Communications
    
    var description: String {
        
        switch self {
        case.Social: return "Social"
        case.Communications: return "Communications" 
        }
    }
}

enum SocialOptions: Int, CaseIterable, CustomStringConvertible {
    
    case editProfile
    case changePassword
    case deleteAccount
    
    var description: String {
        
        switch self {
        case.editProfile: return "Edit Profile"
        case.changePassword: return "Change Password"
        case.deleteAccount: return "Delete Account"
        }

    }

}

enum CommunicationOptions: Int, CaseIterable, CustomStringConvertible {
    
    case notifications
    case language
    case contactUs
    
    var description: String {
        
        switch self {
        case.notifications: return "Notifications"
        case.language: return "Language"
        case.contactUs: return "Contact Us"
        }

    }
    
}

标签: iosswiftxcodeuitableviewenums

解决方案


推荐阅读