Заполнить таблицу просмотра раздела и ячейки - PullRequest
1 голос
/ 03 февраля 2020

Я новичок в Swift и делаю проект для себя, но столкнулся с небольшой проблемой. У меня есть файл с enum для заполнения таблицы. Также расширение в классе для таблицы. Моя проблема в том, что я не могу заполнить таблицу этими перечислениями в другом классе. Ниже я покажу код реализации.

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
    }

}

1 Ответ

1 голос
/ 03 февраля 2020

Вы можете сделать перечисление вот так

  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
    }
}
...