ios – Swift – Ought to I take advantage of enum with Generic or create multiple initializer

0
11


I’m presently dealing with a choice within the activity I’m engaged on. Let me provide you with some context. The display screen Im engaged on is split in 4 sections, every part has its personal occasion of my desk view element, every with particular cells. All 4 desk views are stacked within the display screen. I created a Enum referred to as SectionType and I take advantage of this info when Im instantiating the element like: CustomTableView(ofType: .type1). On this method I can change the enum contained in the desk view resolve which cell Im going to make use of, what number of cells…

Now I have to cross the information to the TableView, I created 4 Fashions and Im pondering how is the easiest way to make use of this in my element.


  • Resolution 1: Creating Comfort inits for every SectionType

My enum:

enum SectionType {
    case information, evaluation, lives, sectors
}

My Customized desk:

personal var newsData: [Model.Home.News]? = nil
personal var analysisData: [Model.Home.Analysis]? = nil
personal var livesData: [Model.Home.Lives]? = nil
personal var sectorsData: [Model.Home.Sector]? = nil

// MARK: Initializers
personal init(for tableViewType: SectionType) {
    self.tableViewType = tableViewType
    tremendous.init(body: .zero, type: .plain)
    delegate = self
    dataSource = self
    registerCells()
}
        
comfort init(for tableViewType: SectionType, with information: [Model.Home.News]) {
    self.init(for: tableViewType)
    self.newsData = information
}

comfort init(for tableViewType: SectionType, with information: [Model.Home.Analysis]) {
    self.init(for: tableViewType)
    self.analysisData = information
}

comfort init(for tableViewType: SectionType, with information: [Model.Home.Lives]) {
    self.init(for: tableViewType)
    self.livesData = information
}

comfort init(for tableViewType: SectionType, with information: [Model.Home.Sector]) {
    self.init(for: tableViewType)
    self.sectorsData = information
}

  • Resolution 2: Making a Generic Let and utilizing Enum with parameters

Altering my Enum to:

enum SectionType {
    case information(information: [Model.Home.News])
    case evaluation(information: [Model.Home.Analysis])
    case lives(information: [Model.Home.Lives])
    case sectors(information: [Model.Home.Sector])
}

And in my desk do one thing like:

personal var information: Array<T>? = nil

// Name this func at initialization
func setData(_ sextionType: SectionType) {
    change sextionType {
    case .information(let information), .evaluation(let information), .lives(let information), .sectors(let information):
        self.information = information
    }
}

I do not know if this resolution is feasible and learn how to make it work


  • Resolution 3: Accepting strategies from you guys

Please remark


Thanks in your consideration S2

LEAVE A REPLY

Please enter your comment!
Please enter your name here