长按单元格时 TableView 单元格背景颜色不会改变

TableView cell background color not changed when long pressed cell

我已将 table 查看单元格选择背景颜色更改为如下。

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

它改变了背景颜色,但是当我长按单元格时,背景颜色保持默认(深灰色)。我想更改按下并长按单元格选择背景颜色。怎么做?

您必须禁用选择样式(因为选择样式包含默认灰色)

cell.selectionStyle = .None

之后添加长按手势和动作。进行所需的编码。

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

tableView.addGestureRecognizer(longpress)

现在,使用以下代码添加 longPressGestureRecognized 函数: 复制

func longPressGestureRecognized(gestureRecognizer: UIGestureRecognizer) {

}

longPressGestureRecognized()函数中,首先获取手势在table视图中的位置和相应的tableViewCell

longPressGestureRecognized()函数中添加如下代码: 复制

let longPress = gestureRecognizer as UILongPressGestureRecognizer

let state = longPress.state

var locationInView = longPress.locationInView(tableView)

var indexPath = tableView.indexPathForRowAtPoint(locationInView)

希望对您有所帮助。