【问题标题】:Passing current UITableViewCell swipe to parent UITableView将当前 UITableViewCell 滑动传递给父 UITableView
【发布时间】:2017-06-12 15:56:32
【问题描述】:

我有一个自定义的UITableViewCell,它具有向右滑动的功能。滑动手势是基于 x 轴的平移,所以,当 x 的平移超过 40 点时,我想触发一个 segue。

我认为这是使用委托传递有关当前 X 值的数据的理想场所。所以,我创建了一个带有函数didSwipeCell()protocol,但我不确定如何将当前x 值传递给UITableView

请告诉我该怎么做,如果您需要任何额外信息,请在评论中告诉我,而不是投反对票。

【问题讨论】:

  • 您是否已经让刷卡代码工作了?你想传递实际的 X 值吗?还是您只想将“didSwipeCell()”发送到 TableViewController?
  • 为什么不向didSwipeCell() 添加包含x 值的参数?
  • @DonMag 我已经可以使用刷卡代码了,我只想发送 X 值。我只是交叉使用委托将 X 值传递给 UITableViewController。
  • 您可以使用“关闭回调”来实现 - 这可能比使用委托更容易/更直接。你能把你的 Cell 的代码贴在你认出 40 个点并准备发送“didSwipeCell()”的地方吗?

标签: ios swift uitableview delegates swift-protocols


【解决方案1】:

添加 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

【讨论】:

  • 谢谢哥们,这正是我要找的(Y)
【解决方案2】:

您可以简单地从您的 UItableViewCell 类到您的 UIViewController 中获取 x 的值。宣布关闭

var getValue: ((_ xValue: CGFloat)->())!

在您的 UItableViewCell 类中并将其实现到您的 UIViewController 类中。

在视图控制器中

cell. getValue = { (xValue) in
// do what you want to do
}

【讨论】:

    【解决方案3】:

    这是另一种方法......

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

    然后您的表格视图单元格设置变为:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      相关资源
      最近更新 更多