enabling didSelect and reorder of a UITableView simultaneously - uitableview

I am trying to get both cell reorder and didSelect UItableView methods to work. Unfortunately, when I set tableView.SetEditing(true, animated: true) the didSelect method does not fire. If I set setEditing to false, I cannot reorder the rows but didSelect does work.
Is there a way to enable both methods simultaneously?

To get both methods to work I added the following code:
tableView.setEditing(true, animated: true)
tableView.allowsSelectionDuringEditing = true

Related

UITableViewCell - distinguish full edit mode and delete row mode

I have UITableViewController and after user pres desired button, I have "full edit" mode. To enable this, I call this:
#objc func btnEditPressed(sender: UIButton) {
sender.isSelected = !self.isEditing
self.setEditing(!self.isEditing, animated: true)
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
for each UITableViewCell, I have override func setEditing(_ editing: Bool, animated: Bool) method. In this, I hide some parts of cell in "full edit" mode.
Now, when the user swipe single table row, I show the delete button. However, in this "delete row mode" I dont want to hide any information from the cell. The problem is, that the same override func setEditing(_ editing: Bool, animated: Bool) for the cell is called as in the first case, leading to hiding cell content.
Is there an easy way, how to solve this, or I have to keep weak reference to UITableViewController and check the mode from the cell myself?
You have two options to handle this.
Check showingDeleteConfirmation in the cell's setEditing method.
Override willTransition(to:) and didTransition(to:) in your cell class.
If your table view is not in editing mode and the user performs a swipe-to-delete gesture, then your cell will experience the following sequence:
willTransition(to:) will be called with the mask including the value UITableViewCell.StateMask.showingDeleteConfirmation
setEditing will be called with editing set to true. The cell's showingDeleteConfirmation will be equal to true
didTransition(to:) will be called with the mask including the value UITableViewCell.StateMask.showingDeleteConfirmation
So the simplest solution is to update your cell's setEditing:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if showingDeleteConfirmation {
// User did a swipe-to-delete
} else {
// The table view is in full edit mode
}
}

How to hide Navigation Bar, when my wkwebview start scrolling?

I am using WKWebview to show my content. When I scroll webview, I would like to hide my navbar. I use below code and nothing is happen.
navigationController?.hidesBarsOnSwipe = true
Can someone tell me, how to do this.
Add top constraint for the webView with superview, set hidesBarsOnSwipe to true, and then load the URL (check screenshot for reference):
One of the ways through code is you can use the scrollview delegate method to identify if webview is scrolled. And there you can use navigationController?.setNavigationBarHidden(isHidden, animated: true).
Something like this:
set scrollview delegate in viewdidload self.webView.scrollView.delegate = self. And then use
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0
{
print("up")
navigationController?.setNavigationBarHidden(false, animated: true)
}
else
{
print("down")
navigationController?.setNavigationBarHidden(true, animated: true)
}
}

hidesBarsOnSwipe method not working when swiping my collection view

I tried implementing the simple one line of code shown below into my viewDidLoad in order to have my navigaion bar hide when a user swipes on my collectionview. However the action only works when i swipe from my navigation bar, no where else. I would like it to hide on swipe from my collection view which of course takes up most of the view.
A mini video of the issue
override func viewDidLoad() {
navigationController?.hidesBarsOnSwipe = true
}
Is the view controller embedded in the navigation controller?
Swift 4, Swift 5:
Hide your navigation bar when you scroll upside and similarly show when you scroll downside.
scrollViewDidScroll() method will get called every time when you scroll your CollectionView, TableView, ScrollView etc.
Try this code:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Check the scroll direction here
if(scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0) {
print("Show")
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.setToolbarHidden(false, animated: true)
}
else {
print("Hide")
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.setToolbarHidden(true, animated: true)
}
}

Why is tableview's setSelected(_:animated:) and setHighlighted(_:animated:) method called on init/load?

Can someone explain to me why the methods setSelected(_:animated:)
and setHighlighted(_:animated:) are called when a TableView is initialized/loaded?
I would have assumed the methods are ONLY called when I actually highlight/select a cell...
I tested this in a TableViewController with three dynamic, custom cells, where I simply print out some text in the method calls:
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
print("setSelected()")
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
print("setHighlighted()")
}
Console output:
setSelected()
setHighlighted()
setSelected()
setHighlighted()
setSelected()
setHighlighted()
setSelected(_:animated:) and setHighlighted(_:animated:) are called every time when a cell in the TableView is initiated. Because the setSelected method is used to set the isSelected property when a cell is selected or not. By default every cell is selected false. To set selected = false, this method is called.
setHighlighted is called every time a cell is initiated. Because every cell is initiated with the isHighLighted property is set to false. To set this value this method is called by default. Whether you set true or false manually or not.
As per the discussion for method in the documentation:
Parameters:
selected:
true to set the cell as selected, false to set it as unselected. The default is false.
animated:
true to animate the transition between selected states, false to make the transition immediate.
So in unselected case that during the loading state, it will be false. If you want to perform any action in case of selection ONLY, add a check for if selected to avoid repetition.

What exactly does setEditing() do in Swift?

I am implementing navigationItem.leftBarButtonItem = editButtonItem under viewDidLoad(), and it is said that I have to implement setEditing(_ editing: Bool, animated: Bool) as well. It seems like every editing functionality works great without setEditing function. What does it do??
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.leftBarButtonItem = editButtonItem
tableView.allowsMultipleSelectionDuringEditing = true
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: true)
tableView.setEditing(tableView.isEditing, animated: true)
}
and it is said that I have to implement setEditing(_ editing: Bool, animated: Bool) as well
Then "it is said" incorrectly.
The built-in editButtonItem of a UITableViewController automatically calls the table view's setEditing for you; there is no need, therefore, to duplicate that functionality. To be more precise:
The built-in editButtonItem of a UIViewController does two things:
It calls the UIViewController's setEditing(_:animated:) when tapped.
It tracks the UIViewController's isEditing property, and changes its own title accordingly (Edit or Done).
Moreover, UITableViewController's implementation of setEditing(_:animated:) calls setEditing(_:animated:) on its table view.
Thus, you would need to do that last step if this were not a UITableViewController. But it is, so you don't.

Resources