1

I am trying to update UITableViewCell's border when the user performs a long press gesture on the cell, but it's not updating.

Here is my cellForRowAtIndexPath: method

let objLongPressHandler = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressHandler(_:)))
objLongPressHandler.view?.tag = indexPath.row
objLongPressHandler.delegate = self
objLongPressHandler.enabled = true
objLongPressHandler.minimumPressDuration = 0.1

cell.contentView.addGestureRecognizer(objLongPressHandler)

This is my function UILongPressGestureRecognizer function.

func longPressHandler(objGesture: UILongPressGestureRecognizer) {

    let center = objGesture.view?.center
    let rootViewPoint = objGesture.view!.superview?.convertPoint(center!, toView: self.tableView)
    let indexPath = self.tableView.indexPathForRowAtPoint(rootViewPoint!)
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as! GroupTableViewCell
    cell.contentView.layer.borderColor = UIColor.redColor().CGColor
    cell.contentView.layer.borderWidth = 3
    cell.setNeedsLayout()
}
Luke Van In
  • 5,215
  • 2
  • 24
  • 45
Murat Kaya
  • 1,281
  • 3
  • 28
  • 52
  • What is `objGesture`? Is it a gesture recogniser? Where is this code being called? What results do you get for each line of the code (i.e. what values do you get for `center`, `rootViewPoint`, `indexPath`, and `cell`) – Luke Van In Jul 12 '16 at 22:37
  • I am getting right indexpath with this code. And I'm callling code in cellforIRowatIndexPath – Murat Kaya Jul 12 '16 at 22:42
  • If I understand correctly - you want the cell border to change when the user performs a long press on the cell. Only the cell which has the long press should change. Is this correct? – Luke Van In Jul 12 '16 at 23:19
  • Perfect, that's much clearer now. I have updated the answer. – Luke Van In Jul 12 '16 at 23:49

1 Answers1

2
  1. Multiple gesture recognisers are not necessary. Use a single long tap gesture recogniser on the main view for the view controller.
  2. When handling the gesture, convert the location to table view coordinates using locationInView. Get the selected row by calling tableView.indexPathForRowAtPoint.
  3. Loop through the visible rows in the table view. If the row is at the selected index path then show the border, otherwise remove the border.

Example:

override func viewDidLoad() {
    super.viewDidLoad()

    // Install tap gesture recogniser on main view.
    let gesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressHandler(_:)))
    gesture.enabled = true
    gesture.minimumPressDuration = 0.1

    view.addGestureRecognizer(gesture)
}

func longPressHandler(gesture: UILongPressGestureRecognizer) {

    // Get the location of the gesture relative to the table view.
    let location = gesture.locationInView(tableView)

    // Determine the row where the touch occurred.
    guard let selectedIndexPath = tableView.indexPathForRowAtPoint(location) else {
        return
    }

    // Iterate through all visible rows.
    guard let indexPaths = tableView.indexPathsForVisibleRows else {
        return
    }

    for indexPath in indexPaths {

        // Get the cell for each visible row.
        guard let cell = tableView.cellForRowAtIndexPath(indexPath) else {
            continue
        }

        // If the index path is for the selected cell, then show the highlighted border, otherwise remove the border.
        let layer = cell.contentView.layer

        if indexPath == selectedIndexPath {
            layer.borderColor = UIColor.redColor().CGColor
            layer.borderWidth = 3
        }
        else {
            layer.borderWidth = 0
        }

        cell.setNeedsLayout()
    }

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    cell.textLabel?.text = "Cell #\(indexPath.row)"
    return cell
}

enter image description here

Luke Van In
  • 5,215
  • 2
  • 24
  • 45
  • but I'll need just set border to selected objects. – Murat Kaya Jul 12 '16 at 23:07
  • Please add more detail to your question to describe what you are trying to accomplish. Could you be more specific? I think you have gesture recogniser somewhere, is that correct? Are you trying to drag something over the table view? Or do you just want a border around the cells that the user has tapped on? – Luke Van In Jul 12 '16 at 23:14
  • I edited my question.And I'm just trying to change border of selected object.But I need the change it in longpresshandler function.So first I getting cell and indexpath with center point and trying to change bordercolor but thats not change. – Murat Kaya Jul 12 '16 at 23:17