2012-04-27 42 views
0

在我的應用程序中有一個UITableView。在表格單元格中放置兩個標籤和一個按鈕。按鈕高度等於(label1 + label2)高度。這些是顯示內容。和按鈕是爲了行動。在這裏我的問題是我無法在所有部分執行按鈕操作。點擊時只有一部分工作。UITableView單元中的按鈕動作

下面是代碼我用來放置標籤和按鈕

artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)]; 
artistButton.backgroundColor = [UIColor redColor]; 
[self.contentView bringSubviewToFront:artistButton]; 
[self.contentView addSubview:artistButton]; 

artistLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 27.0)]; 
artistLabel1.textAlignment = UITextAlignmentLeft; 
artistLabel1.textColor = [UIColor colorWithRed:0.14 green:0.29 blue:0.42 alpha:1]; 
artistLabel1.font = [UIFont boldSystemFontOfSize:15.0]; 
artistLabel1.backgroundColor = [UIColor blueColor]; 
[self.contentView addSubview:artistLabel1]; 

artistLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(11.0,20.0, 170.0, 27.0)]; 
artistLabel2.textAlignment = UITextAlignmentLeft; 
artistLabel2.textColor = [UIColor grayColor]; 
artistLabel2.font = [UIFont boldSystemFontOfSize:12.0]; 
artistLabel2.backgroundColor = [UIColor grayColor]; 
[self.contentView addSubview:artistLabel2]; 

任何一個可以幫助或建議。 在此先感謝。

+0

u能PLZ澄清這行:「按鈕動作中的所有部分」 – sandy 2012-04-27 09:54:22

+0

你需要一個按鈕(它比觸發接觸電池本身另一動作)。你應該使用tableView委託('tableView:didSelectRowAtIndex:')。如果你需要一個按鈕,你應該使用'[UIButton buttonWithType:type]'來創建它。 – calimarkus 2012-04-27 10:37:15

+0

看起來像標籤覆蓋了按鈕框。嘗試將這兩個標籤的內容設置爲按鈕標題。 ' - (void)setTitle:(NSString *)title forState:(UIControlState)狀態; '。您可以調整此「titleLabel」的屬性以使內容出現在多行中。 – 2012-04-27 10:43:31

回答

2

我找到了我的問題的答案。 由於按鈕和標籤的大小隻有它不起作用。 現在我調整了按鈕的寬度和長度,並標記其工作正常。 謝謝大家。

0

添加目標採取行動,這個按鈕

artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)]; 
artistButton.backgroundColor = [UIColor redColor]; 

[artistButton addTarget:self 
      action:@selector(ArtistAction) 
forControlEvents:UIControlEventTouchUpInSide]; 

[self.contentView bringSubviewToFront:artistButton]; 
[self.contentView addSubview:artistButton]; 


-(void)ArtistAction 
{ 
    NSLog("Test Artist Action"); 
} 
+0

我也加了一個動作。執行行動,但我放置兩個標籤。但我的問題是隻有一部分行動正在執行。 – 2012-04-27 10:14:02

0

添加的UIButton聲明前一行..

artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

之後就可以初始化你的UIButton代碼。

0

我想你知不知道你設置重疊的幀。 您確實打電話給[self.contentView bringSubviewToFront:artistButton];,但是您將該按鈕添加爲子視圖後,因此它將不起作用。 您也可以在添加按鈕後添加其他子視圖,這些子視圖將放置在您的按鈕上方。

所以才先添加兩個標籤,然後按鈕,你應該能夠正確地按一下按鈕,雖然你的標籤將是下鍵和不知道有多少人將是可見的。檢查您要添加的對象的幀。

0

只需使用你的tableView:didSelectRowAtIndex:方法。

相關問題