Ios 将当前UITableView单元格滑动传递给父UITableView
我有一个自定义的Ios 将当前UITableView单元格滑动传递给父UITableView,ios,swift,uitableview,delegates,swift-protocols,Ios,Swift,Uitableview,Delegates,Swift Protocols,我有一个自定义的UITableViewCell,它具有向右滑动的功能。滑动手势基于x轴的平移,因此,当x的平移超过40点时,我想触发一个序列 我认为这是使用代理传递有关当前X值的数据的完美场所。因此,我创建了一个带有函数didSwipeCell()的协议,但我不确定如何将当前的x值传递给UITableView 请让我知道怎么做,如果您需要任何额外的信息,请在评论中告诉我,而不是向下投票。添加x值作为didSwipeCell()方法的参数 将委托实例添加到单元格中 class TableViewC
UITableViewCell
,它具有向右滑动的功能。滑动手势基于x轴的平移,因此,当x的平移超过40点时,我想触发一个序列
我认为这是使用代理传递有关当前X值的数据的完美场所。因此,我创建了一个带有函数didSwipeCell()
的协议
,但我不确定如何将当前的x值传递给UITableView
请让我知道怎么做,如果您需要任何额外的信息,请在评论中告诉我,而不是向下投票。添加x值作为didSwipeCell()方法的参数 将委托实例添加到单元格中
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
}
并且,不要忘记在CellForRowatineXpath方法中设置TableViewCell的委托
cell.delegate = self
只需从UItableViewCell类到UIViewController就可以获得x的值。宣告结束
var getValue: ((_ xValue: CGFloat)->())!
在UItableViewCell类中,并将其实现到UIViewController类中
在视图控制器中
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
}
}
}
然后,表格视图单元格设置将变为:
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
}
您的刷卡代码是否已生效?是否要传递实际的X值?或者您只是想将“didSwipeCell()”发送到TableViewController?为什么不向包含
x
值的didSwipeCell()
添加参数?@DonMag我已经有了滑动代码,我只想发送x值。我只是想用委托将X值传递给UITableViewController。你可以用“闭包回调”来实现,这可能比使用委托更简单/更直接。你能把你的手机代码贴在你能识别40个点并且准备好发送“didSwipeCell()”的地方吗?谢谢,伙计,这正是我想要的(Y)
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
}