简体   繁体   English

Swift:如何在TableView中点击时模糊单元格

[英]Swift : how to blurry a cell on tap in tableView

I have a tableView that displays photos. 我有一个显示照片的tableView。 I just want to add a subView in order to make a blur effect on the top of the cell. 我只想添加一个subView,以使单元格顶部具有模糊效果。 My problem is that when i add the subView using didSelectRowAtIndex Method, my subView (my blur) is added to the reused cells... 我的问题是,当我使用didSelectRowAtIndex方法添加subView时,我的subView(模糊)被添加到重用的单元格中...

Here is what i did : 这是我所做的:

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

    let cell:WallTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as WallTableViewCell



    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = cell.imagePosted.bounds


    cell.imagePosted.insertSubview(blurEffectView, atIndex: indexPath.row)


}

Is there a way to add the blur effect using a tap recognizer, so when i tap the blur is added and when i tap a second time the blur is removed ? 有没有一种方法可以使用拍子识别器添加模糊效果,所以当我点击模糊时,当我再次点击模糊时,模糊被消除了? And how to add the blur only on the cell who was tapped ? 以及如何仅在被点击的单元格上添加模糊?

The problem is tableView.dequeueReusableCellWithIdentifier is the wrong method to call here. 问题是tableView.dequeueReusableCellWithIdentifier是在此处调用的错误方法。

You want to call cellForRowAtIndexPath , which is a method on the table view that returns the cell actually being displayed by the table view. 您要调用cellForRowAtIndexPath ,这是表视图上的一种方法,该方法返回表视图实际显示的单元格。 What you're using, tableView.dequeueReusableCellWithIdentifier , provides a "fresh" cell from the re-use queue. 您正在使用的tableView.dequeueReusableCellWithIdentifier提供了重用队列中的“新鲜”单元格。

Also, you almost certainly don't want to add/remove subviews in the didSelectRowAtIndexPath . 另外,您几乎可以肯定不想在didSelectRowAtIndexPath添加/删除子视图。 Instead, you should probably add the blur subviews as part of your original cell design, set them to hidden=true , and then enable them by setting hidden=false . 相反,您可能应该将模糊子视图添加为原始单元格设计的一部分,将其设置为hidden=true ,然后通过设置hidden=false启用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM