0

I have a table view with custom cells and each cell contains multiple UI views.

When a user taps on a UI view inside a cell, the app responds to it based on what UI view is tapped. For example, there are three rows and each row has different number of UI views: (These views might be images, labels to compose different layouts)

row1 - view1  view2  view3
row2 - view4  view5
row3 - view6  view7  view8  view9

If a user taps on row3/view8, the app will detect the tap and know view8 is clicked.

Currently I have two directions to implement this:

(1) Add UITapGestureRecognizer to each UI view

(2) Detect touch on the table view and decide which UI view of visible cells is tapped by calculating which UI view contains the touch point, something like the answer of this question.

I am wondering what the ideal way is? Any other better ways or suggestions?

Community
  • 1
  • 1
Joe Huang
  • 6,296
  • 7
  • 48
  • 81

2 Answers2

1

I would allow the views to detect and handle the taps directly. Seems easiest to me, and this is more the way UIKit was designed to be used. UITapGestureRecognizer seems perfect for this.

Dave Batton
  • 8,795
  • 1
  • 46
  • 50
  • Thanks. Just one question, since there are many rows & UIViews, would it be too much performance overhead to have many gesture recongnizers? – Joe Huang Jan 30 '16 at 07:54
  • 1
    The number of views that need to be updated as the table view scrolls will affect performance. There's lots you can do to optimize this if you're seeing performance problems. For example, try not to use transparent backgrounds or scale anything. The Debug menu in the Simulator app can help you with this. But I wouldn't expect gesture recognizers to be much of a performance hit. – Dave Batton Jan 30 '16 at 18:03
  • In any case, don't optimize prematurely. Write easy to maintain code. Then test. Then if there are performance issues use Apple's tools like the one mentioned above and Instruments to find the problem and go from there. – Dave Batton Jan 30 '16 at 18:05
0

You can use UICollectionView for this. Refer

Arun
  • 1,391
  • 1
  • 10
  • 29
  • No, it's different. These views are images & labels to compose different layouts. I will add this to my question. – Joe Huang Jan 30 '16 at 06:33