I have two action on a table view cell like such:
After I touch them, the actions get executed but the cell goes back to its original state (closed) like such:
How can I prevent the cell from closing and keep show the actions after the touch?
Are you using trailingSwipeActionsConfigurationForRowAt to show the options, if yes then it is the default swipe behavior of UITableViewCell. On any action on the + or - the cell will hide those options, it does not retain it's trailingSwipeActions and goes back to the normal visible cell state.
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
Related
In UITableView, when tapping and holding a cell and (without releasing this cell) switching to another screen in tab bar and returning to the initial one, the cell remains highlighted (not selected) and if tapping on another cell, the first one gets selected.
Upon tapping and holding the first cell, tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) is called for the first cell. So far it is how it should be.
Upon tapping on another cell (after returning to the screen), tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) is called for the first cell. And then all methods for selection for the first cell.
Is there a way to remove this behaviour? And upon returning to the screen have no highlighted cells.
cell.setHighlighted(false, animated: false) doesn't work. Yes, it removes highlighting, but selection methods are again called for the first cell.
Have you tried to implement shouldHighlightRowAt and return false?
From the docs for tableView(_:shouldHighlightRowAt:):
Asks the delegate if the specified row should be highlighted.
I'm trying to implement two cells, one that can be deleted by swiping, and one that can't be deleted, but has swipe actions the other way.
This is what it looks like now:
I don't want the middle cell to be indented when clicking Edit.
Without any custom code, all cells would show this red circle and get a delete-button, but I have added a few lines of code to prevent that.
This code will set all cells without a leading or trailing swipe action to not become editable, which explains the bottom cell in the gif. I have to include the leading swipe actions to this, because if the cell isn't editable, then I can't swipe at all.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
let vm = sections[indexPath.section].viewModels[indexPath.row]
return vm.leadingSwipeActions != nil || vm.trailingSwipeActions != nil
}
The following code will prevent the middle cell from showing the red circle.
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return sections[indexPath.section].viewModels[indexPath.row].trailingSwipeActions == nil ? .none : .delete
}
If I don't pass .none for the cell with leading actions, then it would show the red circle AND it would even show a default deletion-swipe. I don't want the ability to delete this row!
How can I prevent the middle cell from indenting, while still retaining the ability to swipe it?
This method seems to work! tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614873-tableview?language=objc
I'm getting weird behaviour in my TableView
I turned isUserInteractionEnabled on one of my section headers because I need to put a collectionView in it. Before I did anything I noticed that when I tap on that header view (it is a UITableViewCell) it triggers the didSelectRowAt method with the index of a first cell in that section (right below the header). Does anybody know what causes that behaviour and how to turn it off?
Your header sections are part of your UITableView.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
}
In this method indexPath return rows and section, indexPath.section
-> returns section of UitableView, you have tapped and indexPath.row -> returns down of that particular section, tapped.
So this function is going to get called no matter what you do. You
need to override the table view's gesture to avoid this.
I want to customise the delete indicator for a UITableView in editing mode.
Since I can't seem to find a way to customise the appearance style of the delete button shown in the first image, I've created my own animation with my own button.
I have deactivated the red standard button using:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
Now my question: How do I realize the same behaviour as the standard button? If you press the red button, it invokes a swipe left on the cell showing the wanted delete button.
Help is very appreciated.
I have a UITableView that has multi selection enabled. I have been using the "selection" to actually change the height of the rows, showing extra detail when "selected". E.g.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return (self.tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false) ? 200 : 92
}
This seems to work pretty well. Until I start doing any swipes actions. When I add some swipe actions, the swipe action seems to clear all of my selections. I actually wanted to deselect the one I was swiping, so it would shrink back down. But the clearing of all my selections doesn't seem to trigger any of the normal delegate callbacks. Even though I have allowsMultipleSelectionDuringEditing set to true.
Is there a way to do this? Should I skip (ab)using the selection state as a way to indicate whether the row is showing details with a different height or not? Or is there a way to use it in conjunction with the behavior of the swipes being done in "edit mode" and clearing all of my selections?
The best way is using NSArray to store indexPath of selected cells, and base on saved indexPath you can check and do anything you want. another bug may happened in your code is: What happened in the case user make scroll on tableview? Does cell will reuse and lose select state? New cell reuse the old cell with 200 height will has wrong height?