2016-12-14 105 views
0

我想添加滑动像this,实际上这个库是Android的。我想迅速。刷卡撤消像gmail的撤消

我已经使用默认的tableview方法。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 

但问题是它只提供了一种方法,不是左右滑动。

也在GMAIL应用程序有两端刷卡,甚至我们我们刷两次,该行将被自动删除。

请指导我如何做到这一点。我无法找到任何第三方。

有一件事我已经创建添加左侧和右侧手势,然后在他们的方法中添加按钮,但没有发生。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 



     let moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{ 
      action, indexpath in 

      print("MORE ACTION"); 

      self.Selected = "YES" 

      self.selectedValue = indexpath.row 

      self.tableView.rowHeight = 50 

      self.tableView.beginUpdates() 

      let indexPath = NSIndexPath(forRow: indexPath.row, inSection: 0) 

      self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 

      self.tableView.endUpdates() 

     }); 

     moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0); 

     let deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in 
      print("DELETE ACTION"); 

      self.tableView.endUpdates() 

     }); 


    let Action = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "", handler:{action, indexpath in 
     print("ACTION"); 
    }); 

    if self.Selected == "YES" { 

     if self.selectedValue == indexPath.row { 

      return [Action]; 
     } 
     //return [Action]; 
     return [deleteRowAction, moreRowAction]; 
    }else{ 
    return [deleteRowAction, moreRowAction]; 
    } 
} 

并且允许编辑

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 

      return false 
    } 

用于手势

self.tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "customCell") 

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.left(_:))) 


    swipeRight.direction = UISwipeGestureRecognizerDirection.Right 
    self.tableView.addGestureRecognizer(swipeRight) 

    let swipeleft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.right(_:))) 

    swipeleft.direction = UISwipeGestureRecognizerDirection.Left 

    self.tableView.addGestureRecognizer(swipeleft) 



func left(sender: UISwipeGestureRecognizer){ 
      print("left"); 
//what to write inside to make custom button here 
     } 
     func right(sender: UISwipeGestureRecognizer){ 
      print("right"); 
     } 
+0

你为什么不添加自定义单元格里面两种意见(内容,行动)在你刷卡之间切换 – zombie

回答

0

这里所述方法是实现与UISwipeGesture和的UITableViewCell

进口UIKit的该溶液的简单的方法

class TableViewCustomCell:UITableViewCell { 

@IBOutlet weak var mainView: UIView! 


override func awakeFromNib() { 
    let leftSwipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipe(_:))) 
    leftSwipe.direction = .Left; 
    self.contentView.addGestureRecognizer(leftSwipe) 

} 

func swipe(touch: UIGestureRecognizer) 
{ 
    let swipeGesture:UISwipeGestureRecognizer = sender as! UISwipeGestureRecognizer 
    //Write your code to Unread or 
} 
} 
0

一个好的方法将创建一个手势识别触发某个功能,您可以在部署自己的代码:

override func awakeFromNib() {   
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.leftSwiped)) 
    swipeLeft.delegate = self 
    swipeLeft.numberOfTouchesRequired = 1 
    swipeLeft.direction = .left 
    self.tableView!.addGestureRecognizer(swipeLeft) 
} 

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { 
    return true 
} 

func leftSwiped() { 

}