4
votes

I have changed table view cell selection background color as 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 change the background color but when i long press cell then background color remain same as default (dark grey). i want to change press and long press cell selection background color. how to do that ?

1
u can add long press gesture in on cell and its action set cell bg color. - Ilesh P
When did you execute these code? - Bannings
thanks llesh....it works using long press gesture.. - Sweety

1 Answers

3
votes

You have to disable the selection style (because selection style contains default gray color)

cell.selectionStyle = .None

After that add long press gesture and on the action of it. do desired coding.

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

tableView.addGestureRecognizer(longpress)

Now, add the longPressGestureRecognized function with the follow code: Copy

func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {

}

Inside the longPressGestureRecognized() function, start by getting the location of the gesture in the table view and the corresponding tableViewCell.

Add the following code inside the longPressGestureRecognized() function: Copy

let longPress = gestureRecognizer as UILongPressGestureRecognizer

let state = longPress.state

var locationInView = longPress.locationInView(tableView)

var indexPath = tableView.indexPathForRowAtPoint(locationInView)

Hope it helps.