ios – 长按单元格时,TableView单元格背景颜色未更改

我已经更改了表视图单元格选择背景颜色,如下所示.

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()函数内部,首先在表视图中获取手势的位置以及相应的tableViewCell.

在longPressGestureRecognized()函数添加以下代码
复制

let longPress = gestureRecognizer as UILongPressGestureRecognizer

let state = longPress.state

var locationInView = longPress.locationInView(tableView)

var indexPath = tableView.indexPathForRowAtPoint(locationInView)

希望能帮助到你.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...