2010-01-01 48 views
0

我有一個UIScrollView我在那裏創建兩個UIButtons。第一個按鈕的工作原理應該如此,但第二個按鈕消失。它仍然存在並且可以接受點擊,並且當您點擊隱藏按鈕時變得可見。UIScrollView和UIButton,UIButtons消失

關於爲什麼第二個按鈕出現的任何想法?

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 


button_1  = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 

button_1.titleLabel.font       = [UIFont systemFontOfSize:12];; 
button_1.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation; 
button_1.contentVerticalAlignment   = UIControlContentVerticalAlignmentCenter; 
button_1.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentCenter; 

[button_1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[button_1 setTitle:@"Circle" forState:UIControlStateNormal];    
button_1.frame = CGRectMake(0.0, 30.00, 50, 20);  
[button_1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:button_1]; 


button_2  = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 

button_2.titleLabel.font       = [UIFont systemFontOfSize:12];; 
button_2.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation; 
button_2.contentVerticalAlignment   = UIControlContentVerticalAlignmentCenter; 
button_2.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentCenter; 

[button_2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[button_2 setTitle:@"Square" forState:UIControlStateNormal];    
button_2.frame = CGRectMake(0.0, 120.0, 50, 20);  
[button_2 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:button_2]; 
+0

另一個代碼區域導致了這個問題。沒有什麼是你在做什麼樣的代碼錯誤(除了保留是不必要的,但這不會導致你的問題) – bentford

回答

2

你想在這裏實現什麼佈局?默認情況下,表格單元格(除非您有自定義rowHeights44px高。您的button_2將遠離第一個按鈕之下的單元格邊界。此外,沒有理由保留您正在創建的按鈕,並且實際上您不應保留這些按鈕。當你將它們添加到父視圖時,它們將被保留。

+0

@Ben - 謝謝你的答案。是的,我們使用每個單元的計算細胞高度,因此每個細胞的高度都不相同。 我需要在繪製單元格之前計算單元格高度,並在表格單元格中使用多個按鈕。但是,按鈕的位置會在每個單元格中更改。 – Wenzi

+0

當第一個按鈕被擊中時,第二個按鈕是否消失,還是開始隱藏? –

+0

@Ben - 開始隱藏。我曾經用'setHidden = No'來欺騙,只是爲了確定,但沒有幫助。另外,如果您單擊該按鈕,則可以在單擊它時看到它。我也嘗試過UIControlStateHighlighted爲狀態,但這也沒有幫助。 – Wenzi