0

I have a screen in my app that pulls an unknown sized array from my user object, and must display an object for each item in the array that consists of a UIImageView for my background image, a UISwitch to activate/deactivate that particular item, a label with the name of the item, and a label that corresponds to the name of the item.

These are displayed in a UIScrollView, since there are nearly always going to be more items than will fit on the screen, especially on the iPhone 4.

I had initially quickly thrown together a static screen that consisted of 20 of these objects, but have found that I will often be exceeding that number of items in the array, and need to convert to a dynamic solution.

I have not used subclassing at all yet, and want to be sure that this is the proper solution before I dive into the implementation. Adding one subclass to my scrollView sounds a lot cleaner than adding each of those components individually and figuring out the spacing for various screens.

If this is the proper solution, are there any resources you could point me to to learn about how to properly do this subclassing? I.e., how to add a constructor for the objects where I can add the contents of the labels and state of the switch, and programmatically add constraints. I've only used constraints from the storyboard.

If you do not think that subclassing UIView would be the proper way to do this, what other solution would you suggest? I'm admittedly a novice when it comes to iOS development, so I apologize that this post seeks advice on where to start rather than help fixing a specific part of an implementation.

Jake T.
  • 4,308
  • 2
  • 20
  • 48

1 Answers1

1

You should use UITableView or UICollectionView for that, and subclass UITableViewCell or UICollectionViewCell to create your custom view hierarchy. UITableView and UICollectionView will handle reusing of cells which will help in memory management.

Update: as you want to use UITableView so to add spacing between cells set number of sections to number of elements and then add header for section and set its height you can do that by implementing UITableViewDelegates visit below links for detail about UITableView and UITableViewDelegates https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/

Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • Will UITableView allow for the use of my custom background image and spacing between the cells? I use the same background image in other places throughout my app, so I'd like the design to stay consistent. What is the difference between the use case of UITableView and UICollectionView? – Jake T. Aug 10 '15 at 20:34
  • Found this thread, and it appears that UITableView fits my needs, as I need a 1 column list rather than a grid. I am still interested in knowing whether I can create the aesthetics I am looking for with the UITableView. If all of my cells are going to be right on top of each other, i.e. with the background images touching each other, this will not fit my use case. http://stackoverflow.com/questions/23078847/when-to-use-uicollectionview-instead-of-uitableview – Jake T. Aug 10 '15 at 20:37
  • Add imageView in View of view controller and then add UITableView on tap of it, set its background color to clear. Yes you can add spacing as well. UITableView is vertical stack while UICollectionView can be vertical, horizontal and even custom layout. – Adnan Aftab Aug 10 '15 at 20:37
  • I suppose I was unclear. The background image is for each one of my cells, not the entire view. My last question, and I'll be ready to mark you as correct and dig into the UITableView implementation, will the table view act as a scroll view as well if the size of it does not fit inside the View of my UIViewController? Or do I still need to utilize my UIScrollView? – Jake T. Aug 10 '15 at 20:40
  • I updated my answer about adding spacings between cells – Adnan Aftab Aug 10 '15 at 20:42
  • Subclass UITableViewCell and create your custom view, – Adnan Aftab Aug 10 '15 at 20:44