我是Swift的新手程序员,正在为自己做一个项目,但是遇到了一个小问题。我有一个带有枚举的文件来填充表。也是表的类中的扩展。我的问题是我无法在另一个类中用这些枚举填充表格,下面将显示实现代码。
enumsTable.swift
import Foundation
import UIKit
// Section
enum sectionsTable:Int
{
case sectionTableOne
case sectionTableTwo
var titleSectionTable: String {
switch self {
case .sectionTableOne:
return "titleSectionTableOne"
case .sectionTableTwo:
return "titleSectionTableTwo"
}
}
}
// cell in section one
enum cellSectionOne:Int
{
case cellOne
case cellTwo
var titleCellSectionOne:String
{
switch self {
case .cellOne:
return "cellOne"
case .cellTwo:
return "cellTwo"
}
}
}
// icon cell in section one
enum cellIconSectionOne:Int {
case cellOneIcon
case cellTwoIcon
var icon: UIImage {
switch self {
case .cellOneIcon:
return UIImage(named: "iconOne.png")!
case .cellTwoIcon:
return UIImage(named: "iconTwo.png")!
}
}
}
//cell in section two
enum cellSectionTwo:Int
{
case cellOne
case cellTwo
var titleCellSectionTwo:String
{
switch self {
case .cellOne:
return "cellOne"
case .cellTwo:
return "cellTwo"
}
}
}
// icon cell in section two
enum cellIconSectionTwo:Int {
case cellOneIcon
case cellTwoIcon
var icon: UIImage {
switch self {
case .cellOneIcon:
return UIImage(named: "iconOne.png")!
case .cellTwoIcon:
return UIImage(named: "iconTwo.png")!
}
}
}
ViewController.swift
extension ViewController: UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return // ?
}
func numberOfSections(in tableView: UITableView) -> Int {
return // ?
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return //?
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: idCell,for: indexPath) as! ViewControllerTableViewCell
//cellTextLabel ?
//cellImage ?
return cell
}
}
您可以像这样使枚举
enum Menu: Int, CaseIterable {
case Home = 0, Mortgage_Calculate
func description() -> String {
switch self {
case .Home:
return "Home"
case .Mortgage_Calculate:
return "Mortgage Calculate"
}
}
func icon() -> String {
switch self {
case .Home:
return "home"
case .Mortgage_Calculate:
return "calculator"
}
}
func url() -> String {
switch self {
case .Home:
return ""
case .Mortgage_Calculate:
return ""
}
}
}
并像这样使用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RightSlideTableViewCell", for: indexPath) as! RightSlideTableViewCell
cell.lbl_title.text = Menu(rawValue: (indexPath.row))?.description()
let image_name = Menu(rawValue: (indexPath.row))?.icon() ?? ""
cell.image_item.image = UIImage(named: image_name)
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Menu.allCases.count
}