haojianzong/ObjectForm

Suggestion about FormDataSource

Opened this issue · 3 comments

I think FormDataSource should be a concrete class that implements UITableViewDataSource, just like what UITableViewDiffableDataSource does. In this way, developers can use FormDataSource directly:

let fruit: Fruit
let rows: [[BaseRow]]
let dataSource = FormDataSource(model: fruit, rows: rows)
self.tableView.dataSource = dataSource

instead of typing lots of glue code, such as in example:

extension FruitFormVC: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataSource.numberOfSections()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.numberOfRows(at: section)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let row = dataSource.row(at: indexPath)
        row.baseCell.setup(row)
        row.baseCell.delegate = self
        return row.baseCell
    }
}

Thanks for the suggestion. I believe my first iteration tried this but I later regreted it.

Reasons:

  1. FormDataSource works with UITableView most of the time but it should not necessarily be the only scenario. For example, if the user writes custom rows with binding views like UICollectionCell, it is possible to use it with UICollectionView. (Probably we should document this in README)
  2. I personally think cellProvider seem to be a little harder to follow than plain UITableViewDataSource -- it makes me jump back and forth when following the code.

I do like the idea of simple adoption without these glue code. Would it be possible to make subclass(or protocol, I don't know) for FormDataSource, so that it conforms to UITableViewDataSources automatically? If there is PR that we can discuss, it would be great.

Again, thanks for making this discussion to make this project better 👍

Looked at the code, I think I was wrong. Because baseRow must contains a UITableViewCell, there is no reason for the user to use this library in other views than UITableView. Making this library too generic doesn't seem to be a good idea to me.

So I actually think it would be great to implement what you said.

One more thought.

When using ObjectForm, I have come into some scenarios where form rows are not the only rows in my UITableView. For example, I have other dataSource and might want to add code in UITableViewDataSource to provide custom cells or custom headers. Is it possible to achieve with UITableViewDiffableDataSource? The user might have to make a custom BaseRow to add simple cells to UITableView, which would end up with some dirty workarounds.

I am worrying about losing the transparency and take freedom from the user. Maybe a subclass for the user to choose will work.