Mano
Mano

Reputation: 740

How to move labels in ios

enter image description here

Here i need to hide the phone number, email , birthDate, anniversary date and other labels in case there is no values for those fields. How can i do this?

Upvotes: 2

Views: 964

Answers (3)

danh
danh

Reputation: 62686

Many ways, starting with the simplest:

self.emailLabel.hidden = YES;

But you probably want to reformat the other parts of the view to fit the empty space. Keeping it simple, you would then do something like this:

self.phoneLabel.frame = CGRectOffset(self.phoneLabel.frame, 0, -self.emailLabel.bounds.size.height);

... and so on for anything below. But you can see how this would become tedious. The next and probably best alternative is a UITableView that adjusts it's section count based on whether some of that data is present. That would go like this. Prepare a mutable array of arrays with the parts of your model and their values.

- (void)prepareModel {

    self.model = [NSMutableArray array];
    [self.model addObject:@[@"Name", @"Judy"]; // get "Judy" from your data

    if (/* model has email */) {
        [self.model addObject:@[@"Email", @"[email protected]"];  // get email from your model
    }
    // and so on for conditional parts of your model
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.model.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

    return self.model[section][0];
}

And the cellForRow would init the cell using self.model[section][1].

Upvotes: 5

squall leonhart
squall leonhart

Reputation: 21

It is probably a better idea to use a UITableView to do this, by putting the labels in rows of the tables. If the labels are empty, you can delete the table rows and iOS will dynamically resize the table height for you.

Upvotes: 1

IronManGill
IronManGill

Reputation: 7226

What you can do is simply hide the UILabel's if the value for the NSStrings that you are putting them in is NULL/nil .

NSString *labelString;
if([labelString length]>0)
{

}
else
Label.hidden = YES;

Upvotes: 1

Related Questions