【问题标题】:Swipe gesture in tableview在表格视图中滑动手势
【发布时间】:2019-02-14 17:50:52
【问题描述】:

我想在我的单元格上添加滑动手势。它工作正常。但问题是当我在第一个单元格上滑动但我的第五个单元格也滑动时。我知道这个 indexpath 问题。请帮助。但我从几个小时就被困住了。

让 swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleLeftSwipe)) swipeLeft.direction = UISwipeGestureRecognizerDirection.left table.addGestureRecognizer(swipeLeft)

let swipeLeftd = UISwipeGestureRecognizer(target: self, action: #selector(self.handleLeftSwipes))
swipeLeftd.direction = UISwipeGestureRecognizerDirection.right
table.addGestureRecognizer(swipeLeftd)

@objc func handleLeftSwipe(sender: UITapGestureRecognizer) {
    print("rigt called")

    let location = sender.location(in: self.table)
    let indexPath = self.table.indexPathForRow(at: location)
    let cell = self.table.cellForRow(at: indexPath!) as! TableViewCell
    print("swipe")
    cell.myView.frame.origin.x = -94
}

@objc func handleLeftSwipes(sender: UITapGestureRecognizer) {
    print("labelSwipedLeft called")
    let location = sender.location(in: self.table)
    let indexPath = self.table.indexPathForRow(at: location)
    let cell = self.table.cellForRow(at: indexPath!) as! TableViewCell
    print("swipe")
    cell.myView.frame.origin.x =  80
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifier = "TableViewCell"
    var cell: TableViewCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? TableViewCell
    if cell == nil {
        var nib : Array = Bundle.main.loadNibNamed("TableViewCell",owner: self,options: nil)!
        cell = nib[2] as? TableViewCell
    }

    return cell!
}

【问题讨论】:

  • 你能出示你的 cellforindex 吗?
  • @vinbhai4u 请检查
  • 在 cellForRowAtIndexPath 中尝试 tableView.dequeueReusableCell(withIdentifier: , for: )

标签: ios iphone swift


【解决方案1】:

这样做的原因是重复使用您的单元格。我假设您没有在prepareForReuse() 中设置任何内容。所以,你的问题就在cell.myView.frame.origin.x = 80,对吧?只需在 prepareForReuse 或 cellForRowAt indexPath 中将其设置为默认值即可。

【讨论】:

  • 我必须在左滑设置两个不同的框架,另一个用于右滑。我会设置所有两个
  • @varun 是的,只需设置单元格默认视图所需的所有内容,因为当您为一个特定单元格设置它时,然后在滚动期间,该单元格被重用,并且具有子视图的框架像以前一样移动。如果我理解正确,您不希望他们保存移动的框架原点。因此,如上所述,在 prepareForReuse() 中设置默认视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 2014-11-20
相关资源
最近更新 更多