如何从scrollview中删除动态分配的子视图

时间:2012-01-22 14:32:20

标签: ipad dynamic for-loop scrollview subviews

我想删除for循环fron滚动视图中动态分配的子视图。代码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;
}

1 个答案:

答案 0 :(得分:0)

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

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

roc_temp.tag = 1000 * j;

然后使用此代码删除:

[[scrollView viewWithTag:1000] removeFromSuperview];