【问题标题】:show view only when uitableviecell is pressed仅在按下 uitableviecell 时显示视图
【发布时间】:2015-12-17 07:41:08
【问题描述】:

我有一个 UIViewController 和一个 UITableView 和一个覆盖视图。在didSelectRowAtIndexPath 中显示了视图,但我希望它在用户放开屏幕后立即隐藏。因此,仅当用户按下单元格时才会显示视图,并在他松开时立即隐藏。 我尝试使用touchesEnded,但无法正常工作。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.colorView.hidden = false
    self.colorView.backgroundColor = colors[indexPath.row]

}

有什么建议吗?

【问题讨论】:

  • 隐藏你对 touchesEnd 方法的看法 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  • 您是否尝试向单元格添加手势识别器?
  • @CongTran 谢谢你的提示,我试过了,但是当我选择一个表格视图单元格时似乎没有调用 touchesbegan
  • 您是否使用了自定义的 TableviewCell?如果是,您可以在 tableviewcell 上调用此方法,然后将此事件发送到您的 viewcontroller。
  • @CongTran 不,我没有,但我知道如何制作自定义单元格等。但是如何将事件发送到 vc?

标签: ios swift uitableview cocoa-touch


【解决方案1】:

您可以在TableViewCell 上使用touchesEnded 方法来检测用户何时最终触摸此单元格。然后使用委托将此事件发送到您的viewcontroller 例如:

YourTableViewCell.swift

    protocol YourTableViewCellDelegate: class {
        func endTouch(indexPath: NSIndexPath)
    }


    class YourTableViewCell: UITableViewCell {
        weak var delegate: YourTableViewCellDelegate?
        var indexPath: NSIndexPath

       // Add Your Other method
        func bind(index: NSIndexPath) {
             self.indexPath = index
        }

        override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
            self.delegate?.endTouch(self.indexPath)
        }
    }

YourViewController.swift

class YourViewController: UIViewController, YourTableViewCellDelegate {

    // Add Your Other method

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        ...
            cell.delegate = self
            cell.bind(indexPath)
    }

    // MARK: - YourTableViewCellDelegate
    func endTouch(indexPath: NSIndexPath) {
        self.colorView.hidden = true
    }
}

【讨论】:

  • 它有效,谢谢!但是你能告诉我如何检索索引路径(行)吗?
【解决方案2】:

您应该从触摸结束覆盖中调用 colorView.hidden。

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

        self.colorView.hidden = true
}

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多