当我单击正确的UITabButtonItem时,我想将deleteButton添加到我的表视图中,并在单击它时删除行。但是我不知道该怎么做。每次我都会出错。

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
    }

}


这是我的视图控制器

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()
}


}

最佳答案

您必须使用一个函数,使用此代码,您将具有幻灯片功能来擦除行:

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


在这部分代码之后:

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 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)
        }
}


像这样在您的navigationBarButton上调用它:

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

08-26 13:18