Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another Suggestion Needed : Boxing Swift Structs #70

Open
wm-j-ray opened this issue Jun 5, 2016 · 4 comments
Open

Another Suggestion Needed : Boxing Swift Structs #70

wm-j-ray opened this issue Jun 5, 2016 · 4 comments

Comments

@wm-j-ray
Copy link

wm-j-ray commented Jun 5, 2016

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

@wtmoose
Copy link
Member

wtmoose commented Jun 8, 2016

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)
    }
}

@wm-j-ray
Copy link
Author

wm-j-ray commented Jun 8, 2016

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?

@wtmoose
Copy link
Member

wtmoose commented Jun 8, 2016

Sure. They're just captured and used internally by the configuration, so TLIPT has no knowledge of them.

@wm-j-ray
Copy link
Author

wm-j-ray commented Jun 8, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants