I have an iOS app. The main screen has a tabbar on the bottom with three tab bar items. Here I'll explain two related tabs. One of them, the “export” tab, is for displaying objects in a tableview. It allows the user to make multiple selections. Another one, the "edit" tab, is for editing objects. If a user makes any changes to objects, any previously selected object items in the export tab will be cleared if the user switches back to the export tab.
Multiple Selection in Export View
In order to keep selected objects consistant, I created a class called SelcHelper to catch selected objects' indexPath. In ExportViewController, there is a function selectRow which is used to select or deselect a row in the table view.
- class ExportViewController:
- BaseTableViewController // this is my base class on UITableViewController
- {
- private var selHelp[er = SelHelper()
- private func selectRow(
- select: Bool,
- tableView: UITableView,
- at indexPath: IndexPath)
- {
- if select {
- tableView.selectRow(
- at: indexPath,
- animated: true,
- scrollPosition: .none)
- } else {
- tableView.deselectRow(
- at: indexPath,
- animated: true)
- }
- }
- ...
- }
Here is how I implement multiple selection in tableview events:
- class ExportViewController:
- BaseTableViewController // this is my base class on UITableViewController
- {
- ...
- func tableView(
- _ tableView: UITableView,
- didDeselectRowAt indexPath: IndexPath)
- {
- selHelper.remove(indexPath: indexPath)
- tableView.reloadRows(at: [indexPath], with: .none)
- }
- ...
- func tableView(
- _ tableView: UITableView,
- didSelectRowAt indexPath: IndexPath)
- {
- selHelper.add(indexPath: indexPath)
- tableView.reloadRows(at: [indexPath], with: .none)
- }
- func tableView(
- _ tableView: UITableView,
- cellForRowAt indexPath: IndexPath)
- -> UITableViewCell // cellForRow event
- {
- let cell = tableView.dequeueReusableCell(
- withIdentifier: "cell")
- ...
- let isSelected = selHelper.isIndexPathSelected(indexPath)
- cell.isSelected = isSelected
- selectRow(
- select: isSelected,
- tableView: tableView,
- at: indexPath)
- return cell
- }
The above codes work perfectly for multiple selections.
Users may edit objects in the "edit" tab view after they make some selections in the "export" view. As mentioned above, in the case of any objects changed, I want to clear all the selections in the "export" tab view. The following codes are added in the event of viewWillAppear to clear all the catched indexPath values.
- class ExportViewController:
- BaseTableViewController // this is my base class on UITableViewController
- {
- override open func viewWillAppear(
- _ animated: Bool)
- {
- super.viewWillAppear(animated)
- ...
- if myDBModel.dataChanged
- {
- selHelper.clear() // clear all indexPath
- }
- }
- ...
- }
The logic is very clear. In the event of viewWillAppear, if the datasource has any changes, I'll clear all the catched selection indexPath values. This clearance will guarantee the cellForRow call to set cells unselected. Since I use a core data source for my export tableview, the cells with objects changed and in the visible area will be called to reload the cell.
Problem for visible cells
However I found a problem. If the visible cells in the tableView have any selections, the selection marks are gone. I verified that the cellForRow event was called after the event of viewWillAppear and all the selected cells are set to unselected. There is a problem: the cells are still in a high-light status. This status causes the cell to not be selected or unselected again!
For example, here is a cell is selected:
I changed the datasource first, and then back to the export tabview. The viewWillAppear event is called and all selections are set to deselected. The visible cell looks like this: the selection check mark is gone, but it looks like it is still highlighted and cannot be selected again.
I could scroll the tableView to move those visible cells to an un-visible area and scroll back. They are back to their normal status: unselected and not hilighted. However, what if there are only a small number of objects and they cannot be moved out of the tableView? The user would be stuck with this frastration situation and not be able to make a selection again. The user has to kill the app and restart the app to get back to normal.
Found a Soltion!
I think that this situation might be a problem or bug in UIKit. I have been struggling to find a way to resolve the issue for days. After several days and various attempts, suddenly, one evening very late, I found a solution to resolve the issue!
The solution is actually very simple. What I need to do is to call my private function selectRow to clear all selection for the visible cells in the event of viewWillDisappear! This clearance is OK. If there were no data changes, the cellForRow would not be called. If there is any data change, the cached selection indexPath values are cleared in the event of viewWillAppear first, and then cellForRow will be called to deselect visible cells.
- class ExportViewController:
- BaseTableViewController // this is my base class on UITableViewController
- {
- ...
- override func viewWillDisappear(_ animated: Bool)
- {
- super.viewWillDisappear(animated)
- guard let ips = tableViewInBase.indexPathsForSelectedRows
- else { return }
- // deselect all select rows in tableview on exit
- // this will guaranty selection works again on
- // appearance!
- for ip in ips {
- selectRow(select: false,
- tableView: tableViewInBase,
- at: ip)
- }
- }
- ...
- }
References
- My question posted on SO: unselect cell in tableview if the cell is visible
- My iOS App: TapMyNotes
0 comments:
Post a Comment