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

Ipad 如何从scrollview中删除动态分配的子视图,ipad,dynamic,for-loop,scrollview,subviews,Ipad,Dynamic,For Loop,Scrollview,Subviews,我想删除在滚动视图的for循环中动态分配的子视图。代码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 =CGR

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

for(int i=0;i首先:必须释放新的子视图,因为-addSubview:保留它们,因此导致内存泄漏

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

roc_temp.tag = 1000 * j;
然后使用以下代码删除:

[[scrollView viewWithTag:1000] removeFromSuperview];

非常感谢您的帮助,很抱歉稍后回复