将当前的 UITableViewCell 滑动传递给父 UITableView
Passing current UITableViewCell swipe to parent UITableView
我有一个自定义 UITableViewCell
,它具有向右滑动的功能。滑动手势是基于 x 轴的平移,所以,当 x 的平移超过 40 点时,我想触发一个 segue。
我认为这是使用委托传递有关当前 X 值的数据的完美场所。因此,我创建了一个带有函数 didSwipeCell()
的 protocol
,但我不确定如何将当前 x 值传递给 UITableView
。
请让我知道怎么做,如果您需要任何额外信息,请在评论中告诉我,而不是投反对票。
添加 x 值作为 didSwipeCell() 方法的参数
protocol TableViewCellDelegate {
func didSwipeCell(on xValue: Float)
}
将委托实例添加到您的单元格
class TableViewCell: UITableViewCell {
weak var delegate: TableViewCellDelegate?
}
然后,当用户滑动给定 xValue
的单元格时调用该方法
delegate?.didSwipeCell(on: xValue)
在您的 UITableView 中实现 TableViewCellDelegate 方法。
class TableView: UITableView, TableViewCellDelegate {
func didSwipeCell(on xValue: Float) {
//here you can get the xValue
}
并且,不要忘记在您的 cellForRowAtIndexPath 方法中设置 TableViewCell 的委托
cell.delegate = self
您可以简单地从 UItableViewCell Class 到 UIViewController 获取 x 的值。宣布关闭
var getValue: ((_ xValue: CGFloat)->())!
在您的 UItableViewCell Class 中并将其实施到您的 UIViewController Class 中。
在ViewController
cell. getValue = { (xValue) in
// do what you want to do
}
这是另一种方法...
class SwipeTableViewCell: UITableViewCell {
var didSwipeAction : ((CGFloat)->())?
var startLocation = CGPoint.zero
var theSwipeGR: UISwipeGestureRecognizer?
func respondToSwipeGesture(_ sender: UIGestureRecognizer) {
if let swipe = sender as? UISwipeGestureRecognizer {
if (swipe.state == UIGestureRecognizerState.began) {
startLocation = swipe.location(in: self.contentView);
} else if (swipe.state == UIGestureRecognizerState.ended) {
let stopLocation = swipe.location(in: self.contentView);
let dx = stopLocation.x - startLocation.x;
if dx > 40 {
// pass the ending X coordinate in the callback
didSwipeAction?(stopLocation.x)
}
}
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
myInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
myInit()
}
func myInit() -> Void {
if theSwipeGR == nil {
let g = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
g.direction = UISwipeGestureRecognizerDirection.right
self.contentView.addGestureRecognizer(g)
theSwipeGR = g
}
}
}
然后您的 table 视图单元设置变为:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeCell", for: indexPath) as! SwipeTableViewCell
cell.didSwipeAction = {
(xValue) in
print("Simple", indexPath, "X =", xValue)
// call performSegue() or do something else...
}
return cell
}
我有一个自定义 UITableViewCell
,它具有向右滑动的功能。滑动手势是基于 x 轴的平移,所以,当 x 的平移超过 40 点时,我想触发一个 segue。
我认为这是使用委托传递有关当前 X 值的数据的完美场所。因此,我创建了一个带有函数 didSwipeCell()
的 protocol
,但我不确定如何将当前 x 值传递给 UITableView
。
请让我知道怎么做,如果您需要任何额外信息,请在评论中告诉我,而不是投反对票。
添加 x 值作为 didSwipeCell() 方法的参数
protocol TableViewCellDelegate {
func didSwipeCell(on xValue: Float)
}
将委托实例添加到您的单元格
class TableViewCell: UITableViewCell {
weak var delegate: TableViewCellDelegate?
}
然后,当用户滑动给定 xValue
的单元格时调用该方法delegate?.didSwipeCell(on: xValue)
在您的 UITableView 中实现 TableViewCellDelegate 方法。
class TableView: UITableView, TableViewCellDelegate {
func didSwipeCell(on xValue: Float) {
//here you can get the xValue
}
并且,不要忘记在您的 cellForRowAtIndexPath 方法中设置 TableViewCell 的委托
cell.delegate = self
您可以简单地从 UItableViewCell Class 到 UIViewController 获取 x 的值。宣布关闭
var getValue: ((_ xValue: CGFloat)->())!
在您的 UItableViewCell Class 中并将其实施到您的 UIViewController Class 中。
在ViewController
cell. getValue = { (xValue) in
// do what you want to do
}
这是另一种方法...
class SwipeTableViewCell: UITableViewCell {
var didSwipeAction : ((CGFloat)->())?
var startLocation = CGPoint.zero
var theSwipeGR: UISwipeGestureRecognizer?
func respondToSwipeGesture(_ sender: UIGestureRecognizer) {
if let swipe = sender as? UISwipeGestureRecognizer {
if (swipe.state == UIGestureRecognizerState.began) {
startLocation = swipe.location(in: self.contentView);
} else if (swipe.state == UIGestureRecognizerState.ended) {
let stopLocation = swipe.location(in: self.contentView);
let dx = stopLocation.x - startLocation.x;
if dx > 40 {
// pass the ending X coordinate in the callback
didSwipeAction?(stopLocation.x)
}
}
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
myInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
myInit()
}
func myInit() -> Void {
if theSwipeGR == nil {
let g = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
g.direction = UISwipeGestureRecognizerDirection.right
self.contentView.addGestureRecognizer(g)
theSwipeGR = g
}
}
}
然后您的 table 视图单元设置变为:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SwipeCell", for: indexPath) as! SwipeTableViewCell
cell.didSwipeAction = {
(xValue) in
print("Simple", indexPath, "X =", xValue)
// call performSegue() or do something else...
}
return cell
}