0
votes

I'm wondering which is the better way to implement a view which is designed to have complicated subview hierarchy, say a view with one sub view on left and one sub view on right, the left subview has X number of sub-subviews in one column, the right subview has Y number of sub-subviews in a row. (X and Y varies)

Two ways to implement it:

  1. Custom left and right views (ie. UIView subclass), custom left sub-subview, custom right sub-subviews, the root view only deals with custom left and right views, and they configure their sub-subviews
  2. Only one view with a column of views (ie left view's sub-subview) on left and a row of views (ie right view's sub-subview) on right

First approach: pros:

  1. clean hierarchy means better maintainability.
  2. responsibilities distributed over subviews, so less complication in each view

cons:

  1. nested subviews hierarchy
  2. may have worse performance due to auto-layout
  3. delegation chain is more difficult, consider each subview as a button which need to perform certain action, the custom view need to delegate the action all the way back to root view

Second approach: pros:

  1. less subviews
  2. may have better performance
  3. easy delegation chain compared to 1st approach

cons:

  1. hard to maintain / modify, as all subviews are in one level, especially with auto-layout
  2. messy code base since all views are in one base view

Looks like 1st approach is better, but it still has several cons, is there a completely new way to implement it which copes with all the cons?

1

1 Answers

1
votes

Both subviews (left column and right column) must have something in common, otherwise you would not be showing them at the same time. Because they have a general relation I would have no issue going with option 2. With that said, I would still prefer option 1 for the following reasons:

  • A clean hierarchy is easier to understand and maintain.
  • View logic that is distributed is easier to understand and will keep your classes smaller (which also means it will be easier to reuse sub views elsewhere).
  • "May have worse performance" is a big MAYBE. You should take actual measurements with Instruments or by using NSDate and timeIntervalSinceNow. As long as the constraints are always installed at the nearest common ancestor you should be fine.
  • Delegation and target/action won't be so bad self.firstView.subView.button.target = self.

Acceptable performance on all supported hardware should be the primary deciding factor. Maintainability should be a close second.

Go with the first approach.