قالب وردپرس درنا توس
Home / IOS Development / IOS – How do I refactor my custom UITableView to improve maintenance capability

IOS – How do I refactor my custom UITableView to improve maintenance capability



Look at the following structure:

  protocol MyDelegate {
func yourDelegateFunctionForPerson (model: Person)
func yourDelegateFunctionForAnimal (model: Animal)
}


enum CellTypes: String {
case person = "personCell"
case animal = "animalCell"
}

Base Model

  Class BaseModel {
var type: CellTypes

init (type: cell types) {
self.type = type
}
}

Person Model

  Class Person: BaseModel {
was name: String
init (name: string, type: cell types) {
self.name = name
super.init (type: type)
}
}

Animal Model

  class Animal: BaseModel {
was weight: strict
init (weight: string, type: cell types) {
self.weight = weight
super.init (type: type)
}
}

Base Cell

  Class BaseCell: UITableViewCell {
was model: BaseModel?
}

Personal cell

  class PersonCell: BaseCell {
override was model: BaseModel? {
didSet {
guard let model = model like? Person else {fatalError ("Wrong model")}
// Do what you want with this personality
}
}
}

Animal Cell

  class AnimalCell: BaseCell {
override was model: BaseModel? {
didSet {
guard let model = model like? Animals otherwise {fatalError ("Wrong model")}
// Do what you want with this animal body
}
}
}

View Controller

  class ViewController: UIViewController {
@IBOutlet weak was tableView: UITableView!

was list = [BaseModel] ()

override func viewDidLoad () {
super.viewDidLoad ()
setupList ()
}

func setupList () {
la person = Person (name: "John Doe", type: .person)
la animals = animals (weight: "80 KG", type: .imal)
list.append (person)
list.append (animal)
tableView.dataSource = itself
}
}

extension ViewController: UITableViewDataSource {

func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = list [indexPath.row]
let cells = tableView.dequeueReusableCell (withIdentifier: model.type.rawValue, for: indexPath) as! base Cell
cell model = model
cell.delegate = self
return cell
}

func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
}

extension ViewController: MyDelegate {
func yourDelegateFunctionForPerson (Model: Person) {

}

func yourDelegateFunctionForAnimal (Model: Person) {

}


}

The MyDelegate protocol is used to perform "Tap" actions
CellType's enums are used to identify Cell Type and for dequeuing
All the model class will inherit BaseModel which is quite useful, and will eliminate the need to enter cellForRow in function. and all tableViewCells have inherited BaseCell that contains two variables, ie model and delegate. These are overridden in the personal and animal cell.

Edit : Risk of losing Type Security can certainly be reduced if you specify "cell types" directly in super.init () in model class. for example,

  class Person: BaseModel {
was name: String
init (name: string) {
self.name = name
super.init (type: .person)
}
}

As cells are dequeued with "type" variable .. the correct model will be delivered to the correct cell.


Source link