5

テーブルの最初のセルに動詞の翻訳を表示する動詞活用アプリがあります。現在、翻訳リストは単なる文字列(カンマ区切りリスト)ですが、クリック可能なボタンを持つように変更したいと思います。セルビューにボタンを追加することはあまり成功しませんでしたが、カスタムセルでの唯一の経験は特定の配置を使用していたため、セル内でボタンの動的リスト(さまざまな幅)を実現する方法がわかりません。

どんな助けでも大歓迎です。

乾杯。

4

4 に答える 4

4

新しいiPadアプリでこれを行ったので、コードを貼り付けようと思いました。

// NOTE - This is within a loop     
// Add the translation as a button
    UIButton *translationButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    translationButton.backgroundColor = [UIColor clearColor];
    translationButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [translationButton setTitle:translation forState:UIControlStateNormal];
    [translationButton addTarget:self action:@selector(translationSearch:) forControlEvents:UIControlEventTouchUpInside];
    // Work out required size
    fontSize = [translationButton.titleLabel.text sizeWithFont:translationButton.titleLabel.font];      
    CGRect buttonFrame = CGRectMake(xOffset, 15, fontSize.width + 20.0, 24);
    [translationButton setFrame:buttonFrame];

    // Now set the new x Offset
    xOffset = buttonFrame.origin.x + buttonFrame.size.width + 6.0;

    [verbView addSubview:translationButton];    

乾杯。

于 2010-06-16T11:19:30.807 に答える
4

以下を実行

-(CGRect)placeAButton:(NSString*)textFromField withCell:(UITableViewCell*)cell
{
    CGSize      theSize;
    CGSize      constraintSize;

    // Create a button and add as subview to cell
    // Get coordinates to place the button

    UIButton *button    = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

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

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:textFromField forState:UIControlStateNormal];

    UIImage *buttonBkground;
    buttonBkground = [UIImage imageNamed:@"blueButton.png"];
    UIImage *newImage = [buttonBkground stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

[self.tableView reloadData]
}

ボタンを動的に作成するときは、セルの幅、高さなどに注意する必要があります。

于 2009-10-12T04:59:02.750 に答える
3

オブジェクトを作成しUIViewます。UIButtonオブジェクトをこの親ビューのサブビューとして設定します。

それができたら、この親ビューをセルのcontentViewプロパティに追加できます。

-tableView:cellForRowAtIndexPath:デリゲート メソッドでは、セルのcontentView.

プログラムでこれを行う場合、条件が変更されるとすぐに、[tableView reloadData]または同様のメソッドを実行して、テーブル ビューとそのセルを更新します。

于 2009-10-11T11:44:58.207 に答える