Blog

Cell event error [Swift]

Published 1 min read

This post is based on my own guesswork.

If I split the elements that give events to a cell into about two kinds,

there's subclasses of UIControl,

and UIView that doesn't inherit from UIControl.

As for the basis of the split, I don't know whether UIControl subclasses have some separate setup that makes clicks work well when put in a cell,

but even if you put them directly in the cell, the problem I'm about to describe doesn't happen.

The problem is the latter. A cell has a subview called contentView.

And this subview has isUserInteractionEnabled set to true.

Also, to give an event to the latter kind of view,

let iv = UIView() let tap = UITapGestureRecognizer(target: , action: #selector()) iv.isUserInteractionEnabled = true iv.addGestureRecognizer(tap)

you have to use addGestureRecognizer, and in this process too you have to set isUserInteractionEnabled to true.

If you put a view like this directly in the cell, when you click the cell the contentView responds, and it seems no event happens on the view we put in.

So there seem to be about these two solutions.

Either set contentView's isUserInteractionEnabled to false so the view we put in fires the event,

contentView.isUserInteractionEnabled = false

or put the view inside the contentView

contentView.addSubview(iv)

And the latter seems to be the recommended one.

Comments

Leaving a comment stores your nickname (or GitHub profile info). Privacy policy

GitHub