I'm creating a slidable UIView called "containerView" in each cell of a UITableView. However, when i slide a containerView in a specific cell, all containerViews in all cells slide with it. Here is my code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "TestCell") as! TestTableViewCell
cell.containerView.frame = CGRect(x: self.view.frame.size.width - 100, y:200, width: 225, height: 70)
cell.containerView.center.x = self.view.frame.size.width - 100
cell.containerView.backgroundColor = UIColor.black
cell.containerView.layer.cornerRadius = 20
let gesture = UIPanGestureRecognizer(target: self, action: #selector(wasDragged(gestureRecognizer:)))
cell.containerView.addGestureRecognizer(gesture)
cell.containerView.isUserInteractionEnabled = true
gesture.delegate = self
return cell
}
and this is my drag function:
func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
print("wassss dragggggeeed")
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
print(gestureRecognizer.view!.center.x)
let newx = gestureRecognizer.view!.center.x + translation.x
if(newx <= self.view.frame.width + 50) && (newx >= self.view.frame.width - 100) {
gestureRecognizer.view!.center = CGPoint(x: newx, y: gestureRecognizer.view!.center.y)
}else if (newx > self.view.frame.width + 50){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width + 50, y: gestureRecognizer.view!.center.y)
}else if (newx < self.view.frame.width - 100){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width - 100, y: gestureRecognizer.view!.center.y)
}
gestureRecognizer.setTranslation(CGPoint(x: 0,y: 0), in: self.view)
}
}
I want each containerView in each cell to be independent of the other. Any help? Thanks!
Try replacing this code in cellForRowAt method
and in wasDragged(gestureRecognizer: UIPanGestureRecognizer) method