TLIndexPathTools
TLIndexPathTools copied to clipboard
Another Suggestion Needed : Boxing Swift Structs
Tim,
I'm slowing cutting over to Swift from a bunch of ObjC projects that use TLIndexPathTools. I'm a swift noob...
I know you use TTIndexPathTools in a bunch of swift projects. I'm wondering what you have found to be the best/most efficient approach to wrap structs into something that can be used to init a dataModel.
I usually structure a presenter for a viewController that prepares a viewModel...but I'm curious as to what you think would be the best practice for using IndexPathTools and CoreData in a Swift project.
Thanx in advance
You can integrate CoreData using TLTableViewController
as illustrated in the CoreData example project. I don't have any specific recommendations regarding the use of viewModels
because I don't typically use them.
You can put a struct in a Box
using this micro framework: https://github.com/robrix/Box
But lately, I've been using the following items in my data model. It takes a configuration block and so typically, the block captures whatever data I need, including structs, to configure the cell:
public typealias ConfigureViewType = (view: UIView) -> Void
public class ConfigurableIndexPathItem: TLIndexPathItem {
public let configure: ConfigureViewType?
public init!(identifier: NSObject!, configure: ConfigureViewType?) {
self.configure = configure
super.init(identifier: identifier, sectionName: nil, cellIdentifier: nil, data: nil)
}
public init!(identifier: NSObject, sectionName: String?, cellIdentifier: String?, data: AnyObject!, configure: ConfigureViewType?) {
self.configure = configure
super.init(identifier: identifier, sectionName: sectionName, cellIdentifier: cellIdentifier, data: data)
}
}
Here is a typical usage in a subclass of TLTableViewController
:
func updateDataModel(dataItems: [DataModelType]) {
var items: [ConfigurableIndexPathItem] = []
for dataItem in dataItems {
let item = ConfigurableIndexPathItem(identifier: dataItem.id, sectionName: nil, cellIdentifier: CustomCell.identifier, data: nil) { [weak self] in
guard let strongSelf = self else { return }
let cell = $0 as! CustomCell
cell.configureWithDataModel(dataItem)
theme.themeView(cell, type: .SomeThemeType)
}
items.append(item)
}
indexPathController.items = items
}
override func tableView(tableView: UITableView!, configureCell cell: UITableViewCell!, atIndexPath indexPath: NSIndexPath!) {
if let item = indexPathController.dataModel?.itemAtIndexPath(indexPath) as? ConfigurableIndexPathItem {
item.configure?(view: cell)
}
}
So in
upDateDataModel(dataItems: [DataModel])
the array of DataModels, a DataModel could be an object that could contain a struct, a managedObject, flags and the like?
Sure. They're just captured and used internally by the configuration, so TLIPT has no knowledge of them.
One more last one... so let's say you have inside the struct custom cell classes, say five or six different ones. You would have to set the cell identifier in the stock TLIPT item in the configuration block, correct? And I'm assuming that TLIPT is smart enough to read those stock cell identifiers and register the appropriate cell for dequeuing.