【问题标题】:Trying to dynamically add UILabels to a view by doing fast enumeration of an NSMutableArray尝试通过快速枚举 NSMutableArray 将 UILabel 动态添加到视图
【发布时间】:2012-12-17 20:12:36
【问题描述】:

我有一个包含 Person 类型对象的 NSMutableArray。 Person 对象包含 NSString *name、NSString *dateStamp 和 NSString *testScore 的参数。我想使用快速枚举做的是将每个对象的参数显示为视图中的一行上的 UILabel,每个对象都显示在每一行上。

问题在于 NSMutableArray 可能包含任意数量的对象,因此视图上可能有一两行标签,或者视图上可能有几行。我想创建一个 for 循环,该循环将动态填充视图中的每个对象在一行上(并且始终保持间距),并允许用户向下滚动以查看任何更向下且最初无法在屏幕上看到的行.

我的 for 循环如下所示:

for (Person *checkPerson in personList) {
    UILabel *label1 =  [[UILabel alloc] initWithFrame: CGRectMake(10, 10, 50, 20)];
    label1.text = Person.name;

    UILabel *label2 =  [[UILabel alloc] initWithFrame: CGRectMake(70, 10, 50, 20)];
    label2.text = Person.dateStamp;

    UILabel *label3 =  [[UILabel alloc] initWithFrame: CGRectMake(130, 10, 50, 20)];
    label3.text = Person.testScore;

    [self.view addSubView:label1];
    [self.view addSubView:label2]; 
    [self.view addSubView:label3];
 }

我需要做的是动态调整 CGRectMake 字段中的“y”值,以便在迭代 NSMutableArray 时每个对象在另一行上依次向下移动,并且用户可以向下滚动以进一步查看如有必要,增加行。是否自动添加滚动功能?

【问题讨论】:

  • 如果你想在不同的行中显示标签,为什么不使用UITableView

标签: objective-c ios cocoa-touch nsmutablearray fast-enumeration


【解决方案1】:

你应该做块枚举,因为这也给你一个索引,你可以用它来计算y

[personList enumerateObjectsUsingBlock:^(Person *person, NSUInteger idx, BOOL *stop) {
    UILabel *label1 =  [[UILabel alloc] initWithFrame: CGRectMake(10, 10+20*idx , 50, 20)];
    label1.text = person.name;
    UILabel *label2 =  [[UILabel alloc] initWithFrame: CGRectMake(70, 10+20 *idx, 50, 20)];
    label2.text = person.dateStamp;

    UILabel *label3 =  [[UILabel alloc] initWithFrame: CGRectMake(130, 10+20*idx, 50, 20)];
    label3.text = person.testScore;

    [self.view addSubView:label1];
    [self.view addSubView:label2]; 
    [self.view addSubView:label3];
}];

您应该将它们放在 UIScrollView 上,根据数组中元素的数量设置 contentSize。

scrollView.contentSize = CGSizeMake(<width>, [personList count] / 3 *30);

您还应该考虑使用 UITableView。这可能更容易,并且单元重用可以保持较低的内存使用率。

【讨论】:

  • 非常感谢您的回复。 idx 的值是否代表 NSMutableArray 的每个索引?关于 的值,我是保持原样,还是实际上必须指定它?
  • 在枚举元素时将idx传递给块
  • 您还应该考虑使用 UITableView。这可能更容易,并且单元重用可以降低内存使用率。
  • 再次感谢您的详细解决方案。我想我将不得不考虑使用 UITableView 作为更好的选择。 :-)
  • «WWDC 2011, Session 309 — Introducing Interface Builder Storyboarding» 可能对您来说很有趣。它显示了如何轻松地使用子视图填充自定义单元格。
【解决方案2】:

只需在您的代码中添加一个y 变量并在每次迭代后增加它

CGFloat y = 10;
for (Person *checkPerson in personList) {
    UILabel *label1 =  [[UILabel alloc] initWithFrame: CGRectMake(10, y, 50, 20)];
    label1.text = Person.name;

    UILabel *label2 =  [[UILabel alloc] initWithFrame: CGRectMake(70, y, 50, 20)];
    label2.text = Person.dateStamp;

    UILabel *label3 =  [[UILabel alloc] initWithFrame: CGRectMake(130, y, 50, 20)];
    label3.text = Person.testScore;

    [self.view addSubView:label1];
    [self.view addSubView:label2]; 
    [self.view addSubView:label3];

    y+=80;
 }

但正如其他人所说,UITableView 注定要完成这样的任务:D

【讨论】:

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