Instagram / IGListKit

A data-driven UICollectionView framework for building fast and flexible lists.
https://instagram.github.io/IGListKit/
MIT License
12.87k stars 1.54k forks source link

[Question] Tile update/operation #290

Closed mobilinked closed 7 years ago

mobilinked commented 7 years ago

I hope it is ok to ask this here. Otherwise, please feel free to delete this thread.

Purpose

final class CellModel: IGListDiffable { let id: String var name: String ... }

- And in the view controller, the adapter data source is to a variable
```var data: [CellModel] = [...]```
- And in the section control for cells controlling

### Challenges to me
- I tried to update cell content like: 

self.data[0].cells[0].name = "****" adapter.performUpdates(animated: true)

This does not work, as the model object is class object, the value been changed before compare the value by the adapter (adapter has no way to know if something changed) 

- Then tried: 

self.data[0].cells[0].name = "****" adapter.reloadData()

This works, but reload all sections (cells) 

- Then tried: 

let obj = self.data[0].cells[0] obj.name = "****" adapter.reloadObjects([obj])


This works, but reload all the cells in the whole section

### So the question#1
If as preceding not to recreate all the model objects (recreate self.data, just directly change the properties), what's the suitable way to update a specific cell related updated cell model object? 

### question#2
Does IGListKit support to move cell by gesture? If we have to write code to do it, how to update the model object after moving? 

Thanks,
rnystrom commented 7 years ago

I hope it is ok to ask this here

All questions welcome!

what's the suitable way to update a specific cell related updated cell model object?

If you're modifying data that represents something at the cell-level, the modifications and storage should probably happen within the section controller and not up at the view controller. Within the section controller you can then do something like:

self.cells[0].name = "Hello!"
self.collectionContext?.reload(in: self, at: 0)

Does IGListKit support to move cell by gesture?

This is unsupported at the moment, but I'll open an issue to track this because I think its something we could add in the future pretty easily.

mobilinked commented 7 years ago

Thanks much @rnystrom, it works.

And I'd like to see there is delegate to control the moving, like the suggestion:

allowMove(from index: Int) -> Bool
allowMove(to index ...) -> Bool

And allow to disable/enable moving feature.

shuhrat10 commented 7 years ago

I have another question related to that.

If you add or remove CellModels from SectionModel -> [cells], from VC adapter.performUpdates(animated: true) doesn't update the UI. Only reloadData() works. But it's not what I wanted.

How can I update UI for updated cells array in SectionModel from VC.

Thanks!

rnystrom commented 7 years ago

@shuhrat10 performUpdates(animated:,completion:) only works for section-level operations. If you want to do item updates you can do that directly from the section controller using the insert, delete, and reload operations. We're planning on adding moves in #145 for 3.0.0.

To make this more streamlined when your cells are powered by an array of view models, we're also planning #38 so this is more automated.

shuhrat10 commented 7 years ago

@rnystrom That will be awesome, I can't wait to see this functionality.