ios - 使用 UILongPressGestureRecognizer 选择时在 UITableviewCell 周围绘制边框

标签 ios swift uitableview uigesturerecognizer

当用户在单元格上执行长按手势时,我尝试更新 UITableViewCell 的边框,但它没有更新。

这是我的cellForRowAtIndexPath: 方法

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)

这是我的函数 UILongPressGestureRecognizer 函数。

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()
}

最佳答案

  1. 不需要多个手势识别器。在 View Controller 的主视图上使用单个长按手势识别器。
  2. 处理手势时,使用 locationInView 将位置转换为表格 View 坐标。通过调用 tableView.indexPathForRowAtPoint 获取选定的行。
  3. 遍历 TableView 中的可见行。如果该行位于选定的索引路径中,则显示边框,否则移除边框。

例子:

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

关于ios - 使用 UILongPressGestureRecognizer 选择时在 UITableviewCell 周围绘制边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339512/

相关文章:

ios - 如何从 Facebook Graph API GraphRespons 协议(protocol)访问数据

swift - 为数组中的每个对象创建一个 UIButton

ios - 取消选中 Swift4 中的 TableView 所有 TableView 选择

ios - 将节点设置为从一系列点随机生成并以逐渐加快的速度下降

ios - 找不到接受提供的参数 SWIFT 的 “init” 的重载

ios - CGAffineTransform 给出预期的表达式错误

swift 3 .all 已弃用 - 拖放 nstableview

ios - UIRefreshControl 如何检测用户是否开始向下拖动?

iphone - 在 iPhone 的 Objective-C 中的 (commitEditingStyle) 之外调用 (deleteRowsAtIndexPaths)

ios - Swift:如何在 plist 中获取数组的第一个字符串?