Prakash Raman February 2016

UITableView for a list of Controls

I am trying to understand when one should/should not use a UITableView.

I do know, if there is a table of information UITableView would be the perfect fit. But what if there are rows of "controls" ?

See the image below.

enter image description here

What are some of the advantages and disadvantages of using a UITableView to achieve layout above?

What alternatives to a UITableView exist for achieving such a layout?

Answers


Crazyrems February 2016

Having controls in tableViews is perfectly fine.

You may want to know that tableviews have scrollable content and selectable cells, so your interactions will be slightly different than if you directly put your controls in a view.

Row selection and bouncing bounds can be annoying, as well as the delayed tap while touching inside a scroll view.

But if you need your controls to be dynamically shown depending on your settings, it can be done with the tableview data source or delegate methods.


user3847320 February 2016

This comes down to the question of how many controls/rows you will have and what they represent.

Dynamic Number of Controls Based on Model

A tableview is the perfect solution. The complications with having controls in a table view cell that is dynamically generated:

1) You cannot add IBActions for controls in table view cells. You will need to add a target/action to each control when the cell is generated.

2) Your action method will need a way to determine which element in your model the cell is displaying when selected (since cells are reused, controls will correspond to many model objects). To do this you need the index path in your model corresponding to the selected control. This can be done as follows (assuming a button but should work for any control):

Determine the control's position in the tableview:

let buttonPosition = button.convertPoint(.zero, toView: tableView)

Find the index path

let indexPath = self.indexPathForRowAtPoint(buttonPosition)

Then query your model for the object at the index path and respond accordingly.

Finite Number of Controls

The above is needlessly complex if you have a finite and known number of controls at runtime. In that case the above layout can be easily achieved with a UIStackView.

See StackView Documentation

Post Status

Asked in February 2016
Viewed 1,867 times
Voted 6
Answered 2 times

Search




Leave an answer