Objective c 尝试通过快速枚举NSMutableArray,将UILabel动态添加到视图中

Objective c 尝试通过快速枚举NSMutableArray,将UILabel动态添加到视图中,objective-c,ios,cocoa-touch,nsmutablearray,fast-enumeration,Objective C,Ios,Cocoa Touch,Nsmutablearray,Fast Enumeration,我有一个NSMutableArray,其中包含Person类型的对象。Person对象包含NSString*名称、NSString*日期戳和NSString*testScore的参数。我想使用快速枚举将每个对象的参数作为UILabel显示在视图中的一行上,每一个对象都显示在每一行上 问题是NSMutableArray可能包含任意数量的对象,因此视图上可能有一行或两行标签,或者视图上有几行标签。我想创建一个for循环,该循环将使用一条线上的每个对象(且间隔一致)动态填充视图,并允许用户向下滚动以查

我有一个NSMutableArray,其中包含Person类型的对象。Person对象包含NSString*名称、NSString*日期戳和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时,每个对象在另一行上按顺序向下移动,并且如果需要,用户能够向下滚动以进一步查看其他行。滚动功能是自动添加的吗?

您应该执行块枚举,因为这也会为您提供一个索引,您可以使用该索引计算
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);
scrollView.contentSize=CGSizeMake(,[personList count]/3*30);


<>你也应该考虑使用UITababVIEW。这可能更容易,而且单元重复使用可以保持内存使用率较低

只需在代码中添加一个
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

非常感谢您的回复。idx的值是否表示NSMutableArray的每个索引?至于我是否保留它的值,或者我必须指定它吗?在枚举元素的同时将IDX传递给块,还应该考虑使用UITable视图。这可能会更容易,而且单元重复使用会降低内存使用率。再次感谢您提供的详细解决方案。我想我必须考虑使用UITableView作为更好的替代方案。:-)你可能会感兴趣。它展示了如何用子视图轻松填充自定义单元格。如果要在不同的行中显示标签,为什么不使用?