簡體   English   中英

UITableView Cell中的按鈕操作

[英]button action in UITableView Cell

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

這是用於放置標簽和按鈕的代碼

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

任何人都可以提供幫助或建議。 提前致謝。

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

將Target操作添加到此Button

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

在UIButton聲明之前添加一行..

artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

之后,您可以初始化您的UIButton代碼。

我想你確實意識到你設置的幀是重疊的。 你可以調用[self.contentView bringSubviewToFront:artistButton]; 但是你之后添加按鈕作為子視圖它將沒有任何效果。 您還可以在添加按鈕后添加其他子視圖,這些子視圖將放置在您的按鈕上方。

因此,首先添加兩個標簽,然后按鈕,您應該能夠正確地單擊按鈕,但標簽將位於按鈕下方,並且不確定它們中有多少可見。 檢查要為要添加的對象設置的幀。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM