How should I refactor my custom UITableView to improve maintainability

How should I refactor my custom UITableView to improve maintainability

I've got a&nbsp;<code>UITableView</code>&nbsp;with many different kind of views. In each method of the&nbsp;<code>UITableView</code>&nbsp;data source I need to check the type of the cell and type of the object, cast them, and act correctly. This is not very clean (it works) but not very maintainable.

I've got a UITableView with many different kind of views. In each method of the UITableView data source I need to check the type of the cell and type of the object, cast them, and act correctly. This is not very clean (it works) but not very maintainable.

So I was working on something to abstract this part but I'm a little bit stuck. The following code is simplified and maybe not that useful but it is to demonstrate my current problem:

extension UITableView {
    func dequeue<T: UITableViewCell>(_ type: T.Type,
                                     for indexPath: IndexPath) -> T {
        let cell = dequeueReusableCell(withIdentifier: String(describing: type),
                                       for: indexPath)
        guard let cellT = cell as? T else {
            fatalError("Dequeue failed, expect: \(type) was: \(cell)")
        }

    return cellT
}

}

struct Row<Model, Cell> { let view: Cell.Type let model: Model

var fill: ((Model, Cell) -&gt; Void)

}

// Completly unrelated models struct Person { let name: String }

struct Animal { let age: Int }

// Completely unrelated views class PersonView: UITableViewCell {

}

class AnimalView: UITableViewCell {

}

// Usage: let person = Person(name: "Haagenti") let animal = Animal(age: 12)

let personRow = Row(view: PersonView.self, model: person) { person, cell in print(person.name) }

let animalRow = Row(view: AnimalView.self, model: animal) { animal, cell in print(animal.age) }

let rows = [ // personRow animalRow ]

let tableView = UITableView() for row in rows { tableView.register(row.view, forCellReuseIdentifier: String(describing: row.view))

let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.dequeue(row.view, for: indexPath)

row.fill(row.model, cell)

}

The code works, but when I enable the animalRow Swift will complain. This is not that surprising since it cannot resolve the types. I cannot figure out how to get around this.

By using the following code I can declare everything once and execute all the parts like "fill" when I need them. I will also add code like onTap etc, but I removed all this code to keep to problem clear.

ios swift

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

iOS App Development: How Apple's iOS 13 & Swift 5 Will Change App Development Industry

Want to know how Apple's iOS 13 & Swift 5 will change the iOS app development industry? Refer to this blog that contains in-detail information on it.

Easy way to save the class object in user defaults - Swift iOS

Easy way to save the class object in user defaults - Swift iOS Text tutorial: http://adnan-tech.com/easy-way-to-save-the-class-object-in-user-defaults-swift-...

The ultimate guide to iOS Unit Testing with Swift and Xcode

Unit testing is a testing method where you can test “unit” of code whether it is working as you want or not. In Xcode, use XCTest…

How to upload single and multiple images in iOS Swift, PHP and MySQL

How to upload single and multiple images in iOS Swift, PHP, and MySQL.

Advanced Error Handling in Swift 5

try? try! and try Advanced Error Handling in Swift 5