2011-07-12 86 views
2

我有一個視圖由一個標籤和兩個按鈕組成,我命名爲UIView *footerView,因爲我在我的表視圖的最後一部分中實現了這一點。UIButton不能在UItableViewCell中工作

我可以看到細胞內部的觀點:我這個觀點用[cell.contentView addSubview: footerView]

這是我的問題成爲indexPath.section = 3的看法。但我不能點擊按鈕,因爲單元格被選中而不是按鈕(它們在單元格內)。

我該如何解決這個問題? 我的按鈕甚至不可選!

下面的代碼...在cellForRowAtIndexPath:

if (section == 3){ 

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

    if (indexPath.row ==0){ 

     cell.contentMode = UIViewContentModeScaleToFill; 
     [cell.contentView addSubview:self.footerView]; 

    } 

} 

而頁腳視圖(萬一有人需要看到這一點):

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 3, 300, 44)]; 
textView.text = @"Terms and Conditions: Lorem ipsum dolor sit amet, con- sectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut"; 

textView.backgroundColor = [UIColor clearColor]; 
textView.font = [UIFont systemFontOfSize:10]; 

[self.footerView addSubview:textView]; 

//create the button 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

[button setFrame:CGRectMake(0, 60, 300, 44)]; 

//set title, font size and font color 
[button setTitle:@"Create Account" forState:UIControlStateNormal]; 
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:18]]; 
// [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 

//set action of the button 
[button addTarget:self action:@selector(createAccount:) 
forControlEvents:UIControlEventTouchUpInside]; 

[button setEnabled:YES]; 
//add the button to the view 
[self.footerView addSubview:button]; 


UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

[cancelButton setFrame:CGRectMake(0, 107, 300, 44)]; 

//set title, font size and font color 
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal]; 
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]]; 
// [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 

//set action of the button 
[cancelButton addTarget:self action:@selector(cancelPressed:) 
     forControlEvents:UIControlEventTouchUpInside]; 


[cancelButton setEnabled:YES]; 
//add the button to the view 
[self.footerView addSubview:cancelButton]; 

回答

6

您需要實現類似的東西這

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (3 == indexPath.section){ 
     return nil; 
    } 
    return indexPath; 
} 

好吧,如果我執行上面的工作正常。我相信我可以通過使UIButton框架落在單元的contentView之外來重現您的問題。確保單元格足夠大以容納您的標籤,並且它應該正常工作。要使單元更大以適合您的內容,您可能必須執行:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return aHeightThatIsBigEnoughToContainMySubViews; 
} 
+0

TRIED >>>不工作! – Legolas

+1

+1第二位應該解決問題。 –

+0

@paul:我已經照顧到身高問題了。它比我的那個部分的子視圖更大。問題仍未解決。 – Legolas

相關問題