I have a tableView that displays photos. I just want to add a subView in order to make a blur effect on the top of the cell. My problem is that when i add the subView using didSelectRowAtIndex Method, my subView (my blur) is added to the reused cells...
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.You want to call
cellForRowAtIndexPath
, which is a method on the table view that returns the cell actually being displayed by the table view. What you're using,tableView.dequeueReusableCellWithIdentifier
, provides a "fresh" cell from the re-use queue.Also, you almost certainly don't want to add/remove subviews in the
didSelectRowAtIndexPath
. Instead, you should probably add the blur subviews as part of your original cell design, set them tohidden=true
, and then enable them by settinghidden=false
.