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

How to customize background color for swipe left delete action on UITableView using Bond binding? #676

Open
ni79ls opened this issue Mar 13, 2020 · 0 comments

Comments

@ni79ls
Copy link

ni79ls commented Mar 13, 2020

Hi all

I love Bond but I am struggling to use it for my UITableView.

I used to customise the delete action as following without the Bond framework:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
            let delete = UITableViewRowAction(style: .destructive, title: Constants.GlobalConstants.ExerciseSwipeTextDelete) { (action, indexPath) in
                
                self.impactFeedback.impactOccurred()
                self.delegate?.deleteSet(exerciseId: self.exerciseVM?.id ?? "", setId: self.exerciseVM?.sets[indexPath.row].id.value ?? "")
            }
            delete.backgroundColor = Constants.GlobalConstants.TableDeleteBackgroundColor
            return [delete]
    }

Does anybody know to achieve this effect with Bond?

I managed to get the swipe delete action working as following (based on another great post in the list from another user).

1) create an extension:

extension ReactiveExtensions where Base: UITableView {
    var commitItemForRowAt: SafeSignal {
        return dataSource.signal(for: #selector(UITableViewDataSource.tableView(_:commit:forRowAt:))) { (subject: PassthroughSubject, _: UITableView, editingStyle: Int, indexPath: IndexPath) in
            subject.send(indexPath)
        }
    }
}

2) add the following two code segments to the view controller:

self.tableWithSetsView.reactive.dataSource.feed(property: Property(true), to: #selector(UITableViewDataSource.tableView(_:canEditRowAt:)), map: { (value: Bool, _: UITableView, indexPath: IndexPath) -> Bool in
                print("Edit mode selection at : %d", indexPath.row)
                return value
            })
self.tableWithSetsView.reactive.commitItemForRowAt.observeNext { index in
                print("Delete button requesting deletion confirmation at %d !", index.row)
                self.deleteSet(exerciseId: self.exerciseVM?.id ?? "", setId: self.exerciseVM?.sets[index.row].id.value ?? "")
            }

The binding is pretty straightforward itself. Everything works fine except the look is not exactly right. Here is the binding code:

exerciseVM.setsBond.bind(to: tableWithSetsView, rowAnimation: .fade) { dataSource, indexPath, tableView in
            let cell = tableView.dequeueReusableCell(withIdentifier: SetDurationTableViewCell.identifier, for: indexPath) as! SetDurationTableViewCell
            if let exerciseSet = dataSource[indexPath.item] as? SetDurationVM {
                cell.setup(setVM: exerciseSet)
                cell.delegate = self
                return cell
            } else {
                return UITableViewCell()
            }
        }

The big question:

What do I need to add to customise the look of the delete swipe action. I am in particular interested in changing the delete action backgroundColor and potentially the title.

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

1 participant