简体   繁体   English

在表格视图中滑动手势

[英]Swipe gesture in tableview

I want to add Swipe gesture on my cell. 我想在单元格上添加滑动手势。 It work fine.but the Problem is when i swipe on first cell but my fifth cell also swipe. 它工作正常。但是问题是当我在第一个单元格上滑动但我的第五个单元格也滑动时。 I know this indexpath problem. 我知道这个indexpath问题。 Please help.But i am stuck from few hour. 请帮助。但是我从几个小时就被困住了。

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleLeftSwipe)) swipeLeft.direction = UISwipeGestureRecognizerDirection.left table.addGestureRecognizer(swipeLeft) 让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!
}

The reason for that is reusing your cells. 这样做的原因是重复使用您的细胞。 I assume you do not set anything in your prepareForReuse() . 我假设您没有在prepareForReuse()设置任何内容。 So, your problem is in line cell.myView.frame.origin.x = 80 , right? 因此,您的问题出在cell.myView.frame.origin.x = 80 ,对吗? Just set it to default in prepareForReuse or in cellForRowAt indexPath . 只需在prepareForReuse或cellForRowAt indexPath中将其设置为默认值cellForRowAt indexPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM