【问题标题】:Fill table view section and cell填充表格视图部分和单元格
【发布时间】:2020-05-19 05:42:55
【问题描述】:

我是 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
    }

}

【问题讨论】:

    标签: ios swift uitableview enums extension-methods


    【解决方案1】:

    你可以像这样制作枚举

      enum sectionsTable:Int, CaseIterable
    {
        case sectionTableOne
        case sectionTableTwo
    
        var titleSectionTable: String {
            switch self {
            case .sectionTableOne:
                return  "titleSectionTableOne"
            case .sectionTableTwo:
                return  "titleSectionTableTwo"
            }
        }
    }
    
    enum cellSectionOne:Int, CaseIterable
    {
        case cellOne
        case cellTwo
    
        var titleCellSectionOne:String
        {
            switch self {
            case .cellOne:
                return  "cellOne"
            case .cellTwo:
                return  "cellTwo"
    
            }
        }
    
    }
    
    enum cellIconSectionOne:Int, CaseIterable {
    
        case cellOneIcon
        case cellTwoIcon
    
        var icon: UIImage {
            switch self {
            case .cellOneIcon:
                return UIImage(named: "iconOne.png")!
            case .cellTwoIcon:
                return UIImage(named: "iconTwo.png")!
            }
        }
    }
    
    
    enum cellSectionTwo:Int, CaseIterable
    {
        case cellOne
        case cellTwo
    
        var titleCellSectionTwo:String
        {
            switch self {
            case .cellOne:
                return  "cellOne"
            case .cellTwo:
                return  "cellTwo"
    
            }
        }
    
    }
    
    
    enum cellIconSectionTwo:Int, CaseIterable {
    
        case cellOneIcon
        case cellTwoIcon
    
        var icon: UIImage {
            switch self {
            case .cellOneIcon:
                return UIImage(named: "iconOne.png")!
            case .cellTwoIcon:
                return UIImage(named: "iconTwo.png")!
            }
        }
    }
    

    并像这样使用

     extension DetailViewController: UITableViewDataSource, UITableViewDelegate {
    
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return sectionsTable.allCases.count
        }
    
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            sectionsTable.init(rawValue: section)?.titleSectionTable
        }
    
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return cellSectionOne.allCases.count
            }
            else {
                return cellSectionTwo.allCases.count
            }
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
             let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            if indexPath.section == 0 {
                cell.textLabel?.text = cellSectionOne.init(rawValue: indexPath.row)?.titleCellSectionOne
            }
            else {
               cell.textLabel?.text = cellSectionTwo.init(rawValue: indexPath.row)?.titleCellSectionTwo
            }
            return cell
        }
    }
    

    【讨论】:

    • 是的,这是一个很好的例子。但是我需要的正是我写的,我不能改变项目中代码的样式。
    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 2014-01-30
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多