More specifcally, it seems to be repeating the action every time the cell is tapped until I swipe another cell, then it behaves normally.
I've got a UITableView
that lists members of a study list, and I've got swipe actions from both sides. On the leading side I've got Focus/Unfocus
(depending on the current state of the item, and on the trailing side I've got Ignore/Unignore
.
Here's the basics of how I've got things set up:
class StudyListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var swipedRow: Int? // Tracks the last row that was swiped so we can reset it before the view disappears
var listdata: [Any] = [] // Data store. Is a custom data type; this is just a dummy so it's represented in the sample code
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let row = swipedRow { // The navigation view is one tab in a tab controller, so we want to reset the swiped row before switching tabs
let indexPath = IndexPath(row: row, section: 0)
swipedRow = nil // Unset the value. Otherwise if it resets a row and we come back later without swiping, it will try to reset the row a second time, causing visual issues.
tableView.reloadRows(at: [indexPath], with: .automatic) // Reset the row
}
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
swipedRow = indexPath.row
let focus = UIContextualAction(style: .normal, title: "Focus") { [weak self, indexPath] (action, view, boolValue) in
if let self = self {
let listItem = listdata[indexPath.row]
self.swipeFocus(listItem) // Performs list magic, adds to focused items. Does not interact with table view.
self.swipedRow = nil
self.tableView.reloadRows(at: [indexPath], with: .automatic) // Reset the row
}
}
focus.backgroundColor = .systemGreen
let unfocus = UIContextualAction(style: .normal, title: "Unfocus") { [weak self, indexPath] (action, view, boolValue) in
if let self = self {
let listItem = listdata[indexPath.row]
self.swipeUnfocus(listItem) // Performs list magic, adds to focused items. Does not interact with table view.
self.swipedRow = nil
self.tableView.reloadRows(at: [indexPath], with: .automatic) // Reset the row
}
}
unfocus.backgroundColor = .systemOrange
let swipeActions = UISwipeActionsConfiguration(actions: [focus, unfocus])
return swipeActions
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
swipedRow = indexPath.row
let exclude = UIContextualAction(style: .normal, title: "Exclude") { [weak self, indexPath] (action, view, boolValue) in
if let self = self {
let listItem = listdata[indexPath.row]
self.swipeExclude(listItem) // Performs list magic, adds to focused items. Does not interact with table view.
self.swipedRow = nil
self.tableView.reloadRows(at: [indexPath], with: .automatic) // Reset the row
}
}
exclude.backgroundColor = .systemOrange
let unexclude = UIContextualAction(style: .normal, title: "Unexclude") { [weak self, indexPath] (action, view, boolValue) in
if let self = self {
let listItem = listdata[indexPath.row]
self.swipeUnexclude(listItem) // Performs list magic, adds to focused items. Does not interact with table view.
self.swipedRow = nil
self.tableView.reloadRows(at: [indexPath], with: .automatic) // Reset the row
}
}
unexclude.backgroundColor = .systemGreen
let swipeActions = UISwipeActionsConfiguration(actions: [unexclude, exclude])
return swipeActions
}
}
As mentioned, swiping reveals the options appropriately, and swiping on another row closes the first and opens the second. Swiping and tapping a button performs the action as it is supposed to.
Once an action has been performed, however, the button appears to be reset, but remain in an active state—every time I try to swipe on the row or tap on it it repeats the action of the button that was tapped (have verified this via print statements in the console).
The only way to clear this behavior is to swipe on another row, after which it will start behaving properly.
Am I using the right commands to reset the rows, or is there something else I should be using instead?