0

私のアプリには1つのUITableViewがあります。テーブルセルに2つのラベルと1つのボタンを配置しています。ボタンの高さは(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];

誰でも助けたり提案したりできます。前もって感謝します。

4

5 に答える 5

2

私は自分の質問に対する答えを見つけました。ボタンとラベルのサイズが原因で、機能していません。ボタンの幅と長さを調整し、正常に機能するようにラベルを付けます。皆さん、ありがとうございました。

于 2012-04-30T10:15:26.113 に答える
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");
}
于 2012-04-27T09:53:06.407 に答える
0

UIButton宣言の前に1行追加します。

artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

その後、UIButtonコードを初期化できます。

于 2012-04-27T10:03:30.583 に答える
0

設定したフレームが重なっていることに気付いたと思います。電話をかけ[self.contentView bringSubviewToFront:artistButton];ますが、後でサブビューとしてボタンを追加するため、効果はありません。また、ボタンを追加した後に他のサブビューを追加すると、それらはボタンの上に配置されます。

したがって、最初に2つのラベルを追加してから、ボタンを正しくクリックできるはずです。ただし、ラベルはボタンの下にあり、どれだけ表示されるかはわかりません。追加するオブジェクトに設定しているフレームを確認してください。

于 2012-04-27T10:32:52.047 に答える
0

セルtableView:didSelectRowAtIndex:メソッドに使用するだけです。

于 2012-04-28T10:12:35.157 に答える