editButtonItem Does Not Work - ios

I have a table view inside a UIViewController with editing enabled for deleting rows. Swiping from the right lets me delete rows and I have the edit button on the navigation bar but it doesn't actually do anything except switch from saying Edit to Done.
Here is how I'm creating my table view.
override func viewWillAppear(animated: Bool) {
for view in self.view.subviews {
view.removeFromSuperview()
}
if sharedCart.shoppingCart.isEmpty {
self.navigationItem.rightBarButtonItem = nil
isEmptyLabel = UILabel(frame: CGRectMake(self.view.frame.width / 2, self.view.frame.height / 2, self.view.frame.width, self.view.frame.height))
isEmptyLabel.center = self.view.center
isEmptyLabel.text = "Your cart is empty."
isEmptyLabel.textAlignment = .Center
isEmptyLabel.textColor = UIColor.whiteColor()
isEmptyLabel.font = UIFont(name: "Helvetica-Light", size: 20.0)
self.view.addSubview(isEmptyLabel)
} else {
isEmptyLabel.removeFromSuperview()
total = 0
let editItem = self.editButtonItem()
self.navigationItem.rightBarButtonItem = editItem
tableView = UITableView(frame: self.view.frame, style: .Grouped)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = UIColor.clearColor()
tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0)
tableView.registerNib(UINib(nibName: "CartCell", bundle: nil), forCellReuseIdentifier: "passCartCell")
tableView.registerNib(UINib(nibName: "CartFooterView", bundle: nil), forHeaderFooterViewReuseIdentifier: "cartFooter")
self.view.addSubview(tableView)
}
And I use these methods for editing.
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
sharedCart.shoppingCart.removeAtIndex(indexPath.row)
self.viewWillAppear(true)
}
}

Cedric Michael's answer almost works, but it disables editButtonItem's automatic, animated toggling between the Edit and Done title & associated state. The better fix is this:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.setEditing(editing, animated: animated)
}
Also, if you want to hide the Edit button for an empty shopping cart, it would be better form to set navigationItem.rightBarButtonItem = editButtonItem in viewDidLoad() and simply set navigationItem.rightBarButtonItem.isHidden to true or false in viewWillAppear() according to the shopping cart.

For Swift 3, implement this in your UIViewController class:
override func setEditing(_ editing: Bool, animated: Bool) {
if(editing && !tableView.isEditing){
tableView.setEditing(true, animated: true)
}else{
tableView.setEditing(false, animated: true)
}
}
(This is not necessary for a UITableViewController class).
You should also set your editButtonItem before:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = editButtonItem
}
Then it works! ツ

In order for your table to toggle between edit and normal mode on button tap you need to implement
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if self.tableView.editing {
return UITableViewCellEditingStyle.Delete
}
else{
return UITableViewCellEditingStyle.None
}
}
And on button tap or IBAction of button :)
#IBAction func editButtonTapped(sender: AnyObject) {
var button = sender as! UIButton
if button.titleLabel!.text! == "Edit" {
self.tableView.editing = true
button.setTitle("Done", forState: UIControlState.Normal)
}
else{
self.tableView.editing = false
button.setTitle("Edit", forState: UIControlState.Normal)
}
}
EDIT
Just realised you have bar button item on navigation bar rather then plain UIButton :)
So you can modify the IBAction as below :) Make sure the you select System Item for UIBarButton item as Custom and set the title as Edit
#IBAction func editButtonTapped(sender: AnyObject) {
let button = sender as! UIBarButtonItem
if button.title! == "Edit" {
self.tableView.editing = true
button.title = "Done"
}
else{
self.tableView.editing = false
button.title = "Edit"
}
}
This should do the job :) Happy coding :)

Related

unable to dismiss side menu using delegate method in IOS Swift

So, basically, I implemented a SideMenu using the module side menu from pod. however, I am stuck at the last part where when I tapped on any of the options inside the menu, the side menu shall dismiss itself and display corresponding content depending on which menu option I picked.
my code for my main view controller is below:
class Profile: UIViewController, MenuControllerDelegate {
var menu: SideMenuNavigationController?
var menuController:MenuListController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
menu = SideMenuNavigationController(rootViewController: MenuListController())
menu?.leftSide = true
menu?.setNavigationBarHidden(true, animated: false)
SideMenuManager.default.leftMenuNavigationController = menu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
setupNavBaritem()
}
func handleMenuToggle(menuOption: String?) {
print("menuToggle function reached")
menu?.dismiss(animated: true, completion: nil)
}
func setupNavBaritem() {
let menuicon = UIImage(named: "menu")
let menubtn = UIButton(type: .system)
menubtn.setImage(menuicon, for: .normal)
menubtn.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: menubtn)
menubtn.addTarget(self, action: #selector(menubtntapped), for: .touchUpInside)
navigationController?.navigationBar.isTranslucent = false
}
#objc func menubtntapped(){
present(menu!,animated:true)
}
}
then this is my menu controller:
class MenuListController:UITableViewController {
var menuitems = ["1","2","3","4"]
var delegate:MenuControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.register(MenuCell.self, forCellReuseIdentifier: "menucell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuitems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "menucell", for: indexPath) as! MenuCell
let menuetext = menuitems[indexPath.row]
let menuicon = menuimg[indexPath.row]
cell.setmenucell(iconimg: menuicon, text: menuetext)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedMenu = menuitems[indexPath.row]
tableView.deselectRow(at: indexPath, animated: true)
delegate?.handleMenuToggle(menuOption: selectedMenu)
}
}
and this is my protocol:
protocol MenuControllerDelegate {
func handleMenuToggle(menuOption:String? )
}
New updates!
with the delegate setup, i am able to reach the toggle function and print("menuToggle function reached"). but my dismiss doesnt work. i have been searching for a while and seems a lot of ppl are having trouble using the dismiss function to dismiss this sidemenu. any workaround that may work on this? i tried a few, including setting the new root controller, which didnt work
Change the frame of the centeController on delegate call. Set 0 when you want to dismiss.
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
self.centerNavigationController.view.frame.origin.x = self.centerNavigationController.view.frame.width - 80
}, completion: nil)
i missed the delegate in this function
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedMenu = menuitems[indexPath.row]
let temp = Profile()
tableView.deselectRow(at: indexPath, animated: true)
self.delegate = temp
delegate?.handleMenuToggle(menuOption: selectedMenu)
}
now, i can print out my menutoggle function!

UISearchController cancel button dismisses UITabBarController when tapped multiple times in a row

I use a custom search controller that has a tableView for showing the results,
the problem is when tapping the cancel button multiple times it dismisses the tabBarController.
But if i press it normally it acts as it should be.
class UICustomSearchController: UISearchController {
private var suggestedTableView: UITableView!
weak var suggestionDelegate: SearchSuggestionsDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
suggestedTableView = UITableView(frame: CGRect(x: 0, y: searchBar.frame.maxY,
width: view.frame.width,
height: view.frame.height - 70))
suggestedTableView.backgroundColor = UIColor.clear
suggestedTableView.tableFooterView = UIView()
view.subviews.forEach {
if $0.isKind(of: UITableView.self) {
$0.removeFromSuperview()
}
}
view.addSubview(suggestedTableView)
suggestedTableView.dataSource = self
suggestedTableView.delegate = self
suggestedTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
let tap = UITapGestureRecognizer(target: self, action: #selector(tableTapped))
suggestedTableView.addGestureRecognizer(tap)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
suggestedTableView.removeFromSuperview()
suggestedTableView = nil
dismiss(animated: false, completion: nil)
}
func reloadSuggestions() {
suggestedTableView.reloadData()
}
private func dismissView() {
searchBar.text = ""
suggestedTableView.removeFromSuperview()
dismiss(animated: false, completion: nil)
suggestionDelegate?.didDismissed()
}
// MARK: - Actions
#objc func tableTapped(tap:UITapGestureRecognizer) {
suggestedTableView.removeGestureRecognizer(tap)
let location = tap.location(in: suggestedTableView)
let path = suggestedTableView.indexPathForRow(at: location)
if let indexPathForRow = path {
tableView(suggestedTableView, didSelectRowAt: indexPathForRow)
} else {
dismissView()
}
}
}
extension UICustomSearchController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return suggestionDelegate?.suggestions().count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let list = suggestionDelegate?.suggestions() ?? []
cell.textLabel?.text = list[indexPath.row]
cell.textLabel?.textColor = UIColor.color(from: .blueTabBar)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let list = suggestionDelegate?.suggestions() ?? []
searchBar.text = list[indexPath.row]
searchBar.becomeFirstResponder()
suggestionDelegate?.didSelect(suggestion: list[indexPath.row])
}
}
And in the viewController that has search:
func initSearchViews() {
// Create the search controller and specify that it should present its results in this same view
searchController = UICustomSearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.barTintColor = UIColor.white
searchController.searchBar.tintColor = UIColor.color(from: .blueTabBar)
searchController.searchBar.setValue(Strings.cancel.localized, forKey:"_cancelButtonText")
searchController.suggestionDelegate = self
if let searchTextField = searchController!.searchBar.value(forKey: "searchField") as? UITextField {
searchTextField.placeholder = Strings.search.localized
}
// Make this class the delegate and present the search
searchController.searchBar.delegate = self
}
I tried putting this line in viewController but nothing happened:
definesPresentationContext = true

swift: segue on button click

I want to move to the next controller on button click with using segue. I need to get number of press button in next controller.
This is code from my controller:
import UIKit
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tblTable: UITableView!
var buttonTitles = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
override func viewDidLoad() {
super.viewDidLoad()
tblTable.delegate = self
tblTable.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return buttonTitles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "buttoncell") as! ButtonCell
let buttonTitle: String = buttonTitles[indexPath.row]
cell.btnButton.setTitle(buttonTitle, for: .normal)
cell.btnButton.tag = indexPath.row
cell.btnButton.addTarget(self, action: #selector(self.buttonClick(button:)), for: .touchUpInside)
cell.selectionStyle = .none
return cell
}
#objc func buttonClick(button: UIButton) -> Void {
print("btnButton clicked at index - \(button.tag)")
button.isSelected = !button.isSelected
if button.isSelected {
button.backgroundColor = UIColor.green
} else {
button.backgroundColor = UIColor.yellow
}
}
}
class ButtonCell: UITableViewCell {
#IBOutlet var btnButton: UIButton!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
btnButton.backgroundColor = UIColor.green
} else {
btnButton.backgroundColor = UIColor.yellow
}
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
if highlighted {
btnButton.backgroundColor = UIColor.green
} else {
btnButton.backgroundColor = UIColor.yellow
}
}
}
How to solve the problem it with my code?
It's very simple.
Follow these steps to create segue from your tableview cell button (click).
Open your storyboard layout (view controller)
Add new (destination) view controller.
Select your button.
Press & hold control ctrl button from keyboard and drag mouse cursor from your button to new (destination) view controller.
Now add following code to your source view controller file (source code)
-
override func performSegue(withIdentifier identifier: String, sender: Any?) {
print("segue - \(identifier)")
if let destinationViewController = segue.destination as? <YourDestinationViewController> {
if let button = sender as? UIButton {
secondViewController.<buttonIndex> = button.tag
// Note: add/define var buttonIndex: Int = 0 in <YourDestinationViewController> and print there in viewDidLoad.
}
}
}
Another way to handle the same.
You need to use performSegueWithIdentifier("yourSegue", sender: sender) to segue on an event. This takes in your segue identifier in place of "yourSegue".
This will go in the func that you call when the user presses the button. If you need to send the amount of button clicks to the new View Controller then I would do something similar to this:
let secondViewController = segue.destination as! ViewController
secondViewController.buttonClicks = myButtonClicks

Create several buttons

I want to create 10 buttons in the ViewController. These buttons move the user to the next ViewController. If I use a storyboard, do I have to create 10 buttons or is there an easier way to solve the problem?
It should also satisfy following conditions:
my button into cell won't be gray or another colour. But I need to my button will be selected and change colour.
If I use the tableView and press the button, the selected cell fills up with a gray color. I want to select only the button. (Tableview should not show gray color for selection)
Here is sample code as solution to your problem (it's working according to your requirement, just copy and paste in your view controller)
import UIKit
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tblTable: UITableView!
var buttonTitles = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]
override func viewDidLoad() {
super.viewDidLoad()
tblTable.delegate = self
tblTable.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return buttonTitles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "buttoncell") as! ButtonCell
let buttonTitle: String = buttonTitles[indexPath.row]
cell.btnButton.setTitle(buttonTitle, for: .normal)
cell.btnButton.tag = indexPath.row
cell.btnButton.addTarget(self, action: #selector(self.buttonClick(button:)), for: .touchUpInside)
cell.selectionStyle = .none
return cell
}
#objc func buttonClick(button: UIButton) -> Void {
print("btnButton clicked at index - \(button.tag)")
button.isSelected = !button.isSelected
if button.isSelected {
button.backgroundColor = UIColor.green
} else {
button.backgroundColor = UIColor.yellow
}
}
}
class ButtonCell: UITableViewCell {
#IBOutlet var btnButton: UIButton!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
btnButton.backgroundColor = UIColor.green
} else {
btnButton.backgroundColor = UIColor.yellow
}
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
if highlighted {
btnButton.backgroundColor = UIColor.green
} else {
btnButton.backgroundColor = UIColor.yellow
}
}
}
And Snapshot of storyboard layout with tableview and cell interface design
Here is result (working behavior of button) in simulator
I think, this is enough to solve your problem.

How to disable editing UITableViewController in Popover

I turn on the editing table in the FirstViewController
#IBAction func editButtonPressed(sender: UIBarButtonItem) {
self.tableView.allowsMultipleSelectionDuringEditing = true
if self.editing {
let popoverEditMenu = self.storyboard?.instantiateViewControllerWithIdentifier("popoverEditMenu") as! EditMenuTableViewController
popoverEditMenu.modalPresentationStyle = .Popover
popoverEditMenu.popoverPresentationController!.delegate = self
let popover: UIPopoverPresentationController = popoverEditMenu.popoverPresentationController!
popover.barButtonItem = sender
presentViewController(popoverEditMenu, animated: true, completion: nil)
} else {
editButton.image = UIImage(named: "profile_more")
self.editing = !self.editing
}
}
Editing table is included successfully. After the above actions, I want to finish editing, by clicking on a table cell in a popover, code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let firstTableVC = self.storyboard?.instantiateViewControllerWithIdentifier("firstTableVC") as! FirstTableViewController
tableView.deselectRowAtIndexPath(indexPath, animated: true)
switch indexPath.row {
case 0:
self.dismissViewControllerAnimated(true, completion: nil)
firstTableVC.editing = false // Disable Editing
firstTableVC.editButton.image = UIImage(named: "1461294921_15.Pencil")
default:
break
}
}
But there is no change in the button image, and table editing mode not disabled
Solution found!
The problem was solved by the use of delegation. Thanks to #pbasdf for the tip
import UIKit
protocol SecondTableViewControllerDelegate {
func endEditing()
}
class SecondTableViewController: UITableViewController {
var delegate: SecondTableViewControllerDelegate?
...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
switch indexPath.row {
case 0:
self.dismissViewControllerAnimated(true, completion: nil)
delegate?.endEditing()
default:
break
}
}
}
Delegate function in FirstViewController. You need to specify a delegate inheritance in FirstViewController
func endEditing() {
self.editing = false
editButton.image = UIImage(named: "1461294921_15.Pencil")
}
Try to use
firstTableVC.editButton.setImage(UIImage(named:"1461294921_15.Pencil"), forState: UIControlState.Normal)
It must works.

Resources