0

各セルの前に画像ボタンがUITableViewあり、その座標を調整したいUIButton。関連するコードcellForRowは次のとおりです。

 UIImage *image = [UIImage imageNamed "unchecked.png"];
 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 CGRect frame1 = CGRectMake(0.0,0.0, image.size.width, image.size.height);** //changing the coordinates here doesn't have any effect on the position of the image button.
 button.frame = frame1; // match the button's size with the image size 
 [button setBackgroundImage:image forState:UIControlStateNormal]; // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet [button addTarget :self action: @selector(checkButtonTapped:event) forControlEvents:UIControlEventTouchUpInside];
4

2 に答える 2

0

のデフォルトのレイアウトUITableViewCellは [ imageView][ textLabel][ accessoryView] です。それを変更することはできません。

に画像を任意に配置したい場合は、セルのUITableViewCellに a を追加する必要があります。UIImageViewcontentView

于 2012-11-05T12:23:47.447 に答える
0

ビューのフレームを設定すると、スーパー ビューに相対的な位置が設定されるため、フレームを設定する前にボタンをセルのサブビューにする必要があります。

ただし、これは cellForRowAtIndexPath で行うべきではありません。これは、テーブル ビューがセルを「再利用」するたびに新しいボタンを割り当てることを意味するためです。セルごとにボタンを 1 つだけ作成するように、ボタンを作成し、テーブル ビュー セルを開始するときにそのフレームを設定する必要があります。

したがって、必要なのは、このような init メソッドを持つ UITableViewCell サブクラスです。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        UIImage *image = [UIImage imageNamed:@"backgroundImage.png"];
        [self addSubview:button];
        [button setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    }
    return self;
}
于 2012-11-05T12:53:33.940 に答える