2012-01-22 71 views
0

我想刪除for循環fron滾動視圖中動態分配的子視圖。 m代碼難以釋懷的是:如何從滾動視圖中刪除動態分配的子視圖

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; 
} 

回答

0

要首先:你必須釋放你的新的子視圖,如-addSubview:保留它們,因此你造成內存泄漏。

要回答:設置子視圖的「標籤」屬性,像這樣:

roc_temp.tag = 1000 * j; 

,然後刪除使用此代碼:

[[scrollView viewWithTag:1000] removeFromSuperview]; 
+0

非常感謝您的幫助和響應遺憾後來 – rehan

+0

沒問題。你總是可以upvote和/或接受:) – 2012-01-30 10:41:16