maxsokolov / TableKit

Type-safe declarative table views.
MIT License
706 stars 74 forks source link

How to activate ability to delete a row? #91

Open wdcurry opened 5 years ago

wdcurry commented 5 years ago

I am likely TableKit so far, but am used to more manual styles for setting up row deletion (and Parse). I find no examples of this within TableKit, only refs to .CanDelete but am not sure how to structure the code.

I am more interested in integrating Dwiff into TableKit but thought I should master this side first.

maxsokolov commented 5 years ago

Hi @wdcurry,

Here is an example of row deletion:

let row = TableRow<MyCell>(item: /*...*/)

let canDeleteAction = TableRowAction<MyCell>(.canDelete) { (options) in
    return true
}

let deleteAction = TableRowAction<MyCell>(.clickDelete) { [weak self] (options) in

    self?.tableDirector.sections[options.indexPath.section].delete(rowAt: options.indexPath.row)
    self?.tableView.beginUpdates()
    self?.tableView.deleteRows(at: [options.indexPath], with: .automatic)
    self?.tableView.endUpdates()
}

row.on(canDeleteAction)
row.on(deleteAction)

tableDirector += row

Hope it helps!

wdcurry commented 5 years ago

This is awesome, thank you. I must have had a typo as I tried the original "return true" but received a compile error. Once I get tested, I will consider Dwiff as it is just so slick, as is TableKit.

wdcurry commented 5 years ago

Perhaps my bits are not up-to-date, as I cannot assign a return value to canDeleteAction. This is what through me off the trail to begin with. The workflow is a bit unintuitive as the delete action in canDeleteAction is more an attribute than an action. Perhaps an evolution to allow us to pass in an array of attributes might make this tighter?

My pod line is: pod 'FileKit' and the laptop is new so I should have the latest. I trust my cell definition is not involved in the issue as it is only a T ..

edit: I had dropped a print() within the canDeleteAction closure and that was throwing it off, I can now proceed ;) to test.

wdcurry commented 5 years ago

For completeness, as I typically work with arrays of records that feed my tables, I ended up with for video in self.recentVideos { rows.append( TableRow<RecentVideoTableViewCell>(item: video, actions: [action, canDeleteAction, deleteAction]) ) }. *yes, my formatting skills here suck at present ;)

maxsokolov commented 5 years ago

The workflow is a bit unintuitive as the delete action in canDeleteAction is more an attribute than an action.

The original idea behind all those actions is how UITableViewDelegate operates with that. Take a quick look at shouldHighlightRowAt/willSelectRowAt and all other methods. It's just one delegate and so many responsibilities...

At the moment of implementing I just followed simplicity and wrapped everything under Action kind of thing. Probably, it could have been done better by grouping actions some how, or even introduce some kind of Attributes. But I don't have such plans. It's good idea to document things better anyway.

wdcurry commented 5 years ago

Thanks Max, you did a great job, and I like it. Once I get a bit of time, I will see about integrating Dwiff into the mix, which should allow even greater ease. The idea being that by simply managing the actual data itself will then be reflected into the tableview, which is a true plumber's helper.

Majituteniyazov commented 4 years ago

@maxsokolov Здравия желаю. Подскажи пожалуйста как изменять название "кнопки" удаления по свайпу с помощью TableKit ? Мне надо локализацию добавить

maxsokolov commented 4 years ago

@maxsokolov Здравия желаю. Подскажи пожалуйста как изменять название "кнопки" удаления по свайпу с помощью TableKit ? Мне надо локализацию добавить

@Majituteniyazov привет! Из коробки этой функции нет, придется наследоваться от TableDirector и добавить ему в наследнике реализацию метода. Чтобы управлять title можно будет реализовать кастомный action. См исходник как там реализованы методы data source и delegate.

Majituteniyazov commented 4 years ago

@maxsokolov хорошо, спасибо, постараюсь сделать

@maxsokolov Здравия желаю. Подскажи пожалуйста как изменять название "кнопки" удаления по свайпу с помощью TableKit ? Мне надо локализацию добавить

@Majituteniyazov привет! Из коробки этой функции нет, придется наследоваться от TableDirector и добавить ему в наследнике реализацию метода. Чтобы управлять title можно будет реализовать кастомный action. См исходник как там реализованы методы data source и delegate.

Majituteniyazov commented 4 years ago

@maxsokolov Приветствую! Мы уже как-то общались на тему кастомного тайтла выше. Подскажи можно ли как-либо кастомизировать TableRowAction ? Я хотел бы добавить туда пару других еще экшенов

maxsokolov commented 4 years ago

@maxsokolov Приветствую! Мы уже как-то общались на тему кастомного тайтла выше. Подскажи можно ли как-либо кастомизировать TableRowAction ? Я хотел бы добавить туда пару других еще экшенов

Не очень понимаю, что значит кастомизировать? Лучше рассмотреть на примере, что требуется сделать.

Majituteniyazov commented 4 years ago

@maxsokolov Приветствую! Мы уже как-то общались на тему кастомного тайтла выше. Подскажи можно ли как-либо кастомизировать TableRowAction ? Я хотел бы добавить туда пару других еще экшенов

Не очень понимаю, что значит кастомизировать? Лучше рассмотреть на примере, что требуется сделать.

Я хочу добавить TableRowActionType и реализацию (как пример: .on(.canDelete) и .on(clickDelete) ) я хочу добавить также .on(.editAction) и его реализацию

Majituteniyazov commented 4 years ago
Снимок экрана 2020-05-13 в 14 22 42
maxsokolov commented 4 years ago

Я хочу добавить TableRowActionType и реализацию (как пример: .on(.canDelete) и .on(clickDelete) )

Как вариант можно использовать .custom("")

invoke(action: .custom("my_action") ... )

.on(.custom("my_action") ... )