2015-06-09 49 views
0

当我点击右边的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() 
} 

}

+0

你并不需要手动添加手势识别 –

+0

同样适用于按钮 –

回答

1

有,你必须使用一个功能,有了这个代码,你将有幻灯片功能来擦除行:

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:

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:")) 
+0

如果(self.tableView.editing)不起作用 – compbealov

+0

@compbealov你是什么意思不起作用?文档说: “确定接收器是否处于编辑模式的布尔值。” 如果表正在编辑,则为true;如果不是,则为false,我尝试并正在工作。你能告诉我什么Xcode说 –

+0

(UITableView,numberOfRowsInSection:Int) - > Int'没有一个名为'编辑'的成员 – compbealov

相关问题