0

我想删除在 for loop 滚动视图中动态分配的子视图。代码 m tryin 是:

for (int i=0; i<[appDelegate.NO_KPI count]; i++) {
static float j = 0;     
roc_temp = [[UIView alloc]init];
roc_temp.frame = CGRectMake(81, 57 + j, 193, 119);
label1 = [[UILabel alloc] init];
label1.frame =CGRectMake(0, 0, 193, 26);
label1.text = @"Budget";
[roc_temp addsubview:label1];
[scrollview addSubview:roc_temp];

roc_temp1 = [[UIView alloc]init];
roc_temp1.frame = CGRectMake(318, 57 + j, 193, 119);
label11 = [[UILabel alloc] init];
label11.frame =CGRectMake(0, 0, 193, 26);
label11.text = @"Actual";
[roc_temp1 addsubview:label11];
[scrollview addSubview:roc_temp1];
j+=166;
}
4

1 回答 1

0

首先:您必须释放新的子视图,因为 -addSubview: 保留它们,因此您会导致内存泄漏。

回答:设置子视图的“标签”属性,如下所示:

roc_temp.tag = 1000 * j;

然后使用以下代码删除:

[[scrollView viewWithTag:1000] removeFromSuperview];
于 2012-01-22T14:43:52.260 回答