我想在单元格上添加滑动手势。它工作正常。但是问题是当我在第一个单元格上滑动但我的第五个单元格也滑动时。我知道这个indexpath问题。请帮助。但是我从几个小时就被困住了。
let swipeLeft = UISwipeGestureRecognizer(目标:自我,动作:#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!
}
答案 0 :(得分:1)
原因是重复使用您的单元格。我假设您没有在prepareForReuse()
中设置任何内容。因此,您的问题在cell.myView.frame.origin.x = 80
行中,对吗?只需在prepareForReuse或cellForRowAt indexPath
中将其设置为默认值即可。