【问题标题】:Create Two UILabels dynamically using Objective C使用 Objective C 动态创建两个 UILabel
【发布时间】:2016-12-16 06:08:35
【问题描述】:

在一个数组中,我有一个字符串和一个数组,我想在自定义单元格中动态显示它。我的数组结构如下所示。它会根据来自网络服务的数据而变化。

{
subDescription =     (
    "Worship Hours 5.00am to 12.30pm & 4.00pm to 10.00pm",
    "The temple is open all days of the week"
);
subHeading = Hours;
}

我试过这样使用这种格式我可以显示subHeading 值只如何显示subDescription 数组

float xCoordinate=10.0,yCoordinate=30.0,width=320,height=40;
float ver_space=20.0;

for (int i = 0; i <test.count; i++)
{
  NSDictionary *lanuage=[test objectAtIndex:i]; 
  NSString *work=[lanuage objectForKey:@"subHeading"];
  NSArray *er=[lanuage objectForKey:@"subDescription"];

  UILabel *label =  [[UILabel alloc] initWithFrame:   
  CGRectMake(xCoordinate,yCoordinate,width,height)];

  label.text = work;
  [events.contentView addSubview:label]; 
  yTCoordinate=yTCoordinate+heightT+ver_space;
}

【问题讨论】:

  • 您的问题标题具有误导性。您是要动态创建两个标签,还是在检索 subDescription 时遇到问题???
  • 我想动态创建两个标签,获取它对我来说没问题。创建 subHeading 后,我需要显示 subDescription 数组元素。

标签: ios objective-c arrays uilabel custom-cell


【解决方案1】:

我不确定我是否理解,但你为什么不使用 CollectionView 而不是标签。在那里您可以定义标题单元格和子单元格。我认为这符合您的问题。

如果您不想使用集合视图,您可以调整代码并通过循环遍历您的子项数组。

    for (NSString *item in er) {
       UILabel *label =  [[UILabel alloc] initWithFrame:   
       CGRectMake(xCoordinate,yCoordinate,width,height)];

       label.text = item;
       [events.contentView addSubview:label]; 
       yCoordinate=yCoordinate+height+ver_space; 
    }

【讨论】:

    猜你喜欢
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多