varun
varun

Reputation: 317

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. 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)

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

Upvotes: 0

Views: 429

Answers (1)

Bliss
Bliss

Reputation: 575

The reason for that is reusing your cells. I assume you do not set anything in your prepareForReuse(). So, your problem is in line cell.myView.frame.origin.x = 80, right? Just set it to default in prepareForReuse or in cellForRowAt indexPath.

Upvotes: 1

Related Questions