Julian Silvestri
Julian Silvestri

Reputation: 2027

Hiding UILabel based on TableView Selection

I am having a hard time figuring out this problem. ******Note: I am using a split view controller***** I am attempting to hide UILabels based on the value of a tableView cell that is selected.

Example:

Below is the tableView I have and when I selected a cell data populates in the detailView. enter image description here

DetailView

enter image description here

Essentially I have too much data that needs to be presented differently based on what cell is selected.

In the detail view you can see that there is no data for 'From' and 'Receiver'

How would I go about hiding those UILabels and their counterparts (meaning the UILabel where the data is populated beside).

Is this even possible ?

Thanks!

Upvotes: 1

Views: 70

Answers (1)

Chris
Chris

Reputation: 4391

In viewDidLoad() (or wherever you setup the detail view), simply hiding empty labels will mean the UIStackView will slide the remaining labels into position. You will need an IBOutlet to the labels of course (or have generated them in code).

Here is an example with a variable receiver which is either nil or has data that would go into the "Receiver" field:

if receiver != nil {
    receiverNameLabel.isHidden = false
    receiverDataLabel.isHidden = false
    receiverLabel.text = "Text from your data here"
} else {
    receiverNameLabel.isHidden = true
    receiverDataLabel.isHidden = true
    receiverLabel.text = ""

The UISTackView will do the rest. If the left and right sections are in two separate stack views, make sure they have the same settings (fill etc) so that when a "row" of labels disappears, the others remain aligned.

Upvotes: 1

Related Questions