ConfigurableTableViewController
  • October 5, 2023

Simple view controller that provides a way to configure a table view with multiple types of cells while keeping type safety. To learn what and whys I encourage you to read the associated blog post.

Usage


Let’s say we want to present a table view controller with four rows: two with text and two with images. We start by creating view data structures that cells will be configurable with:

struct TextCellViewData {
let title: String
}

struct ImageCellViewData {
let image: UIImage
}

and the cells themselves:

class TextTableViewCell: UITableViewCell {
func updateWithViewData(viewData: TextCellViewData) {
textLabel?.text = viewData.title
}
}

class ImageTableViewCell: UITableViewCell {
func updateWithViewData(viewData: ImageCellViewData) {
imageView?.image = viewData.image
}
}

Now to present a table view controller with those cells, we simply configure it in the following way:

import ConfigurableTableViewController

let viewController = ConfigurableTableViewController(items: [
CellConfigurator<TextTableViewCell>(viewData: TextCellViewData(title: “Foo”)),
CellConfigurator<ImageTableViewCell>(viewData: ImageCellViewData(image: apple)),
CellConfigurator<ImageTableViewCell>(viewData: ImageCellViewData(image: google)),
CellConfigurator<TextTableViewCell>(viewData: TextCellViewData(title: “Bar”)),
])

presentViewController(viewController, animated: true, completion: nil)

And ta-da 🎈:

I encourage you to check both the implementation and an example app.

Demo


To run the example project; clone the repo, open the project and run ExampleApp target.

Requirements


iOS 8 and above.

GitHub


View Github

#ios8 #swift #tableview #viewcontroller
YOU MIGHT ALSO LIKE...
exyte

     

camerakit-ios

CameraKit helps you add reliable camera to your app quickly. Our open source camera platform provides consistent capture results, service ...

HybridCamera

[video width="192" height="416" mp4="https://swiftgit.com/wp-content/uploads/2024/12/68747470733a2f2f7261776769742e636f6d2f7374796c656b69742f696d672f6d61737465722f7669645f6564697465645f325f326d622e676966.mp4"][/video]

TakeASelfie

An iOS framework that uses the front camera, detects your face and takes a selfie. This api opens the front ...

iOS-Depth-Sampler

Code examples of Depth APIs in iOS