How can I change the image in the right button of the navigation bar when I select a specific cell?
A good example is in the picture where the rightBarButtonItem
image is different based on the selected cell:
You can simply add a right bar button item in the navigation bar with this code in the viewDidLoad:
let navBarbutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.bookmarks, target: self, action: nil)
navItem.rightBarButtonItem = navBarbutton
(change the systemItem with your image)
Add an IBOutlet like this:
@IBOutlet weak var navItem: UINavigationItem!
and connect it to the navigation item in your storyboard.
Then in your didSelectRowAtIndexPath you can handle it with this sample code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (indexPath.row == 0)
{
navItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: nil)
}
else if (indexPath.row == 1)
{
navItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.camera, target: self, action: nil)
}
}