0%

Enum表示TableView的数据源

//
// SettingTableViewController.swift


import UIKit

protocol SettingViewControllerDelegate: class {
    func controllerDidChangeDateMode(controller: SettingTableViewController)
    func controllerDidChangeTemperatureMode(controller: SettingTableViewController)
}

class SettingTableViewController: UITableViewController {

    weak var changeDelegate: SettingViewControllerDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = 44
    }
}

extension SettingTableViewController {
    private enum Section: Int {
        case date
        case temperature
        
        var numberOfRows: Int {
            return 2
        }
        
        static var count: Int {
            return Section.temperature.rawValue + 1
        }
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return Section.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let section = Section(rawValue: section) else {
            fatalError("未知section索引")
        }
        return section.numberOfRows
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return "Date format"
        }
        return "Temperature unit"
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: SettingTableViewCell.reuseIdentifier, for: indexPath) as? SettingTableViewCell else {
            fatalError("未知的tableViewCell")
        }
        guard let section = Section(rawValue: indexPath.section) else {
            fatalError("未知的section")
        }
        switch section {
        case .date:
            cell.label.text = (indexPath.row == 0) ? "Fri, 01 December" : "F, 12/01"
            let timeMode = UserDefaults.dateMode()
            if indexPath.row == timeMode.rawValue {
                cell.accessoryType = .checkmark
            } else {
                cell.accessoryType = .none
            }
        case .temperature:
            cell.label.text = (indexPath.row == 0) ? "Celcius" : "Fahrenheit"
            let temperatureMode = UserDefaults.temperatureMode()
            if indexPath.row == temperatureMode.rawValue {
                cell.accessoryType = .checkmark
            } else {
                cell.accessoryType = .none
            }
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        guard let section = Section(rawValue: indexPath.section) else {
            fatalError("未知的section索引")
        }
        switch section {
            case .date:
                let dateMode = UserDefaults.dateMode()
                guard indexPath.row != dateMode.rawValue else { return }
                if let newMode = DateMode(rawValue: indexPath.row) {
                    UserDefaults.setDateMode(to: newMode)
                }
                changeDelegate?.controllerDidChangeDateMode(controller: self)
            case .temperature:
                let temperatureMode = UserDefaults.temperatureMode()
                guard indexPath.row != temperatureMode.rawValue else { return }
                if let newMode = TemperatureMode(rawValue: indexPath.row) {
                    UserDefaults.setTemperatureMode(to: newMode)
                }
                changeDelegate?.controllerDidChangeTemperatureMode(controller: self)
        }
        let sections = IndexSet(integer: indexPath.section)
        tableView.reloadSections(sections, with: .none)
    }
}

提供一种思路

代码来自泊学

–EOF–