TableView cell background color did not change when long cell pressed

I changed the background color of the table cell as shown below.

var cell = tableView.cellForRowAtIndexPath(indexPath)

        let selectionColor = UIView() as UIView
        selectionColor.layer.borderWidth = 1
        selectionColor.layer.borderColor = utility.uicolorFromHex(0xEBEBEB).CGColor
        selectionColor.backgroundColor = utility.uicolorFromHex(0xEBEBEB)
        cell!.selectedBackgroundView = selectionColor

      

it changes the background color, but when I press the button for a long time, the background color remains the same as the default (dark gray). I want to change the press and click the cell's cell highlight color. how to do it?

+3


source to share


1 answer


You must turn off the selection style (since the selection style is gray by default)

cell.selectionStyle = .None

      

After that add a long gesture for an action. do the encoding you want.

let longpress = UILongPressGestureRecognizer(target: self, action: "longPressGestureRecognized:")

tableView.addGestureRecognizer(longpress)

      

Now add the function longPressGestureRecognized

with the following code: Copy



func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {

}

      

Inside the function, longPressGestureRecognized()

start by locating the gesture in the table view and matching tableViewCell

.

Add the following code inside the function longPressGestureRecognized()

: Copy

let longPress = gestureRecognizer as UILongPressGestureRecognizer

let state = longPress.state

var locationInView = longPress.locationInView(tableView)

var indexPath = tableView.indexPathForRowAtPoint(locationInView)

      

Hope it helps.

+2


source







All Articles