Проведите жестом в виде таблицы - PullRequest
0 голосов
/ 10 сентября 2018

Я хочу добавить жест Swipe в свою ячейку. Это работает нормально. Но проблема в том, когда я провожу пальцем по первой ячейке, но моя пятая ячейка также сильно ударить. Я знаю эту проблему indexpath. Пожалуйста, помогите. Но я застрял от нескольких часов.

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

1 Ответ

0 голосов
/ 10 сентября 2018

Причиной этого является повторное использование ваших клеток. Я предполагаю, что вы ничего не установили в своем prepareForReuse(). Итак, ваша проблема в линии cell.myView.frame.origin.x = 80, верно? Просто установите его по умолчанию в prepareForReuse или в cellForRowAt indexPath.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...