简体   繁体   English

如何在ios中移动标签

[英]How to move labels in ios

在此输入图像描述

Here i need to hide the phone number, email , birthDate, anniversary date and other labels in case there is no values for those fields. 在这里,我需要隐藏电话号码,电子邮件,birthDate,周年纪念日和其他标签,以防这些字段没有值。 How can i do this? 我怎样才能做到这一点?

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. 下一个,也许是最好的替代方法是UITableView,它根据是否存在某些数据来调整其节数。 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", @"judy@gmail.com"];  // 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] . 而cellForRow将使用self.model[section][1]初始化单元格。

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 . 如果要放入的NSStrings的值为NULL / nil那么您可以简单地隐藏UILabel's

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

}
else
Label.hidden = YES;

It is probably a better idea to use a UITableView to do this, by putting the labels in rows of the tables. 通过将标签放在表的行中,使用UITableView来实现这一点可能是个更好的主意。 If the labels are empty, you can delete the table rows and iOS will dynamically resize the table height for you. 如果标签为空,则可以删除表格行,iOS将动态调整表格高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM