0

When I click the right UITabButtonItem i want to add deleteButton to my table view and delete the rows when i click it. But i don't know how to do this. In each time i get errors.

import UIKit

class CustomCell: UITableViewCell {

    @IBOutlet weak var label: UILabel!

    @IBOutlet weak var view: UIView!

    var gesture: Bool!

    override func awakeFromNib() {
        super.awakeFromNib()

        var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
        var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

        leftSwipe.direction = .Left
        rightSwipe.direction = .Right

        self.addGestureRecognizer(leftSwipe)
        self.addGestureRecognizer(rightSwipe)

        gesture = false;


    }


    func handleSwipes(sender:UISwipeGestureRecognizer) {

        if (gesture == false){

            if (sender.direction == .Left) {

                gesture = true;

                var labelPosition = CGPointMake(self.view.frame.origin.x - 55.0, self.view.frame.origin.y);

                UIView.animateWithDuration(0.5, animations: {

                    self.view.frame = CGRectMake( labelPosition.x , labelPosition.y , self.view.frame.size.width, self.view.frame.size.height)

                }, completion: { (value: Bool) in

                    self.gesture = false;

                })
            }

            if (sender.direction == .Right) {

                gesture = true;

                var viewPosition = CGPointMake(self.view.frame.origin.x + 55.0, self.view.frame.origin.y);

                UIView.animateWithDuration(0.5, animations: {

                    self.view.frame = CGRectMake( viewPosition.x , viewPosition.y , self.view.frame.size.width, self.view.frame.size.height)

                }, completion: { (value: Bool) in

                    self.gesture = false;

                })
            }

        }
    }

    func buttonClick(sender : UIButton) {

        let deleteButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
        let image = UIImage(named: "delete") as UIImage?
        deleteButton.frame = CGRectMake(0, 0, 51, 46)
        deleteButton.setImage(image, forState: .Normal)
        deleteButton.backgroundColor = UIColor.redColor()
        deleteButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(deleteButton)

    }


    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)


    }

    func setCell(labeltext :String)
    {
        self.label.text = labeltext
    }

}

here is my view controller

class controller: UIViewController, UITableViewDelegate,  UITableViewDataSource {

var arrayOfList: [List] = [List]()


override func viewDidLoad() {
    super.viewDidLoad()


    var editButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "edit"), style: UIBarButtonItemStyle.Plain, target: self, action: "")

    editButton.tintColor = UIColor.whiteColor()

    var settingsButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "settings"), style: UIBarButtonItemStyle.Plain, target: self, action : "")

    settingsButton.tintColor = UIColor.whiteColor()

    self.navigationItem.leftBarButtonItem = settingsButton

    self.navigationItem.rightBarButtonItem = editButton


    self.setUpList()
}


func setUpList()

{
    var list1 = List(name:"Apple")
    var list2 = List(name:"Banana")
    var list3 = List(name:"Orange")
    var list4 = List(name:"Pineapple")
    var list5 = List(name:"Tomato")
    arrayOfList.append(list1)
    arrayOfList.append(list2)
    arrayOfList.append(list3)
    arrayOfList.append(list4)
    arrayOfList.append(list5)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfList.count
}


func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell

    let list = arrayOfList[indexPath.row]
    cell.setCell(list.name)

    return cell

}

func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    return true
}

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
                }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

2
  • You don't need to manually add gesture recognizer Commented Jun 9, 2015 at 8:38
  • Same applies for the buttons Commented Jun 9, 2015 at 8:40

1 Answer 1

1

There is a function you have to use, with this code you will have the slide function to erase rows:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
   if editingStyle == UITableViewCellEditingStyle.Delete {
       self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
   }
}

After this part of your code:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfList.count
}

func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell

    let list = arrayOfList[indexPath.row]
    cell.setCell(list.name)

    return cell
}

You should add this func:

func editClicked(sender: AnyObject?) {
        if (self.tableView.editing) {
            let editbutton = UIBarButtonItem(image: UIImage(named: "trash"), style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("editClicked:"))
            self.navigationItem.rightBarButtonItem = editbutton
            self.tableView.setEditing(false, animated: true)
        } else {
            let editbutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("editClicked:"))
            self.navigationItem.rightBarButtonItem = editbutton
            self.tableView.setEditing(true, animated: true)
        }
}

An call it on your navigationBarButton like this:

var editButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "edit"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editClicked:"))
Sign up to request clarification or add additional context in comments.

6 Comments

@compbealov what do you mean doesn't work? Documentation says: "A Boolean value that determines whether the receiver is in editing mode." True if table is editing and false if is not, i try it and is working. can you tell me what Xcode says
(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'editing'
@compbealov you got my code inside the func numberOfRowsInSection? In your code use: var editButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "edit"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editClicked:"))
It is other fun: insert it after numberOfRowsInSection, like this: func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrayOfList.count } func editClicked(sender: AnyObject?) { ... } And call to the func on your navigationButton like this: var editButton : UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "edit"), style: UIBarButtonItemStyle.Plain, target: self, action:Selector("editClicked:")) Hope this fix your problem @compbealov
Victor thank you very much because you try to fix my problem but there is still the same error / '(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'editing' :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.