目前,
我在我的应用程序中实现了一项功能,该功能可以在 UIView 的子视图中动态分配具有 2 个 UILabel 的 UIViews ......我之前已经这样做了,但由于某种原因似乎被困在了这个问题上。
这是我的代码:
UIView *view;
UILabel *label;
UILabel *powerOutput;
for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++)
{
view = [[UIView alloc] initWithFrame:CGRectMake((i-1)*100 + 5, 10, 90, 100)];
view.backgroundColor = [UIColor blackColor];
label = [[UILabel alloc] init];
label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:10];
label.textColor = [UIColor whiteColor];
powerOutput.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
[powerOutput setTag:LABEL_TAG+i];
NSLog(@"%@", label.text);
[view addSubview:label];
powerOutput = [[UILabel alloc] init];
powerOutput.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 50, view.frame.size.width - 10, 20);
powerOutput.backgroundColor = [UIColor clearColor];
powerOutput.font = [UIFont systemFontOfSize:10];
powerOutput.textColor = [UIColor whiteColor];
powerOutput.textAlignment = NSTextAlignmentCenter;
powerOutput.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
[powerOutput setTag:IMAGE_TAG+i];
NSLog(@"%@", powerOutput.text);
[view addSubview:powerOutput];
[self.view addSubview:view];
}
for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++) {
UILabel *label = (UILabel *)[self.view viewWithTag:LABEL_TAG+i]; // get the label with tag
//NSLog(@"%@", label);
label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
//NSLog(@"%@", label.text);
UILabel *powerOutputLabel = (UILabel *)[self.view viewWithTag:IMAGE_TAG+i]; // get the label with tag
powerOutputLabel.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
//NSLog(@"%@", powerOutputLabel.text);
}
我也试过:
UIView *view;
UILabel *label;
UILabel *powerOutput;
for (int i = 1; i < [[_detailsArray objectAtIndex:1] count]; i++)
{
view = [[UIView alloc] initWithFrame:CGRectMake((i-1)*100 + 5, 10, 90, 100)];
view.backgroundColor = [UIColor blackColor];
label = [[UILabel alloc] init];
label.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 2, view.frame.size.width - 10, 20);
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:10];
label.textColor = [UIColor whiteColor];
powerOutput.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:@"SN %@", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"sn"]];
//[powerOutput setTag:LABEL_TAG+i];
NSLog(@"%@", label.text);
[view addSubview:label];
powerOutput = [[UILabel alloc] init];
powerOutput.frame = CGRectMake(view.frame.origin.x + 5, view.frame.origin.y + 50, view.frame.size.width - 10, 20);
powerOutput.backgroundColor = [UIColor clearColor];
powerOutput.font = [UIFont systemFontOfSize:10];
powerOutput.textColor = [UIColor whiteColor];
powerOutput.textAlignment = NSTextAlignmentCenter;
powerOutput.text = [NSString stringWithFormat:@"%@W", [[[_detailsArray objectAtIndex:1] objectAtIndex:i] objectForKey:@"Pout_W"]];
//[powerOutput setTag:IMAGE_TAG+i];
NSLog(@"%@", powerOutput.text);
[view addSubview:powerOutput];
[self.view addSubview:view];
}
这不应该有任何问题,因为当我创建一个新视图时,我会创建一个新的 UILabel 并将其添加到 NEW UIView ...
这是一张照片:
所以我创建了三个视图,好吧……但是我的 3 个 UILabel 在哪里……还有一个文本的日志声明:
2013-07-16 18:14:26.945 PowerOneApp[14307:c07] SN 875222
2013-07-16 18:14:26.945 PowerOneApp[14307:c07] 53.50W
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] SN 875225
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] 59.89W
2013-07-16 18:14:26.946 PowerOneApp[14307:c07] SN 957188
2013-07-16 18:14:26.947 PowerOneApp[14307:c07] 135.39W
真的不知道为什么这不能正常工作......有人有什么想法吗?在这一点上,这似乎是一件非常微不足道的事情,就像一行代码之类的......我在使用具有多个这样的视图的标签之前已经这样做了,并且对于不同的 UIViews 具有不同的值,并且 UILabels 值随着关于我实现的 UISlider。
所以我对这个问题有一些经验,但我现在很困惑。也许明天我会明白我做错了什么,但目前......我真的不确定......如果有人可以指点......请这样做:)