如何在uitableviewcell中添加ImageButton

时间:2011-04-27 12:41:35

标签: ios objective-c uitableview

我正在开发一个应用程序,我想在tableviewcell和tableviewcell中添加一个imagebutton 已经有一个图像(尺寸为320 * 103的渐变图像),我想在单元格上添加一个图像按钮。我们正在添加,但按钮不显示。

我的代码是:

UIImageView *cellimg = [[UIImageView alloc ]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 103.0)];
cellimg.image = [UIImage imageNamed:@"eventListcellimage .png"];
[self.view addSubview:cellimg];
registerButton = [[UIButton alloc]init];//WithFrame:CGRectMake(210, 65, 90, 25)];
registerButton.frame = CGRectMake(210.0, 65.0, 90, 25);
registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[registerButton setImage:[UIImage imageNamed:@"register.png"] forState:UIControlStateNormal];
[registerButton addTarget:self action:@selector(registerButtonAction) forControlEvents:UIControlEventTouchUpInside];
[cellimg addsubView:registerButton];

1 个答案:

答案 0 :(得分:1)

为您解决此问题。你不仅要重新分配按钮,而且还泄漏了cellimg。

UIImageView *cellimg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 103.0)];
cellimg.image = [UIImage imageNamed:@"eventListcellimage.png"];
cellimg.userInteractionEnabled = YES; //button won't work without this
[self.view addSubview:cellimg];
[cellimg release];

registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
registerButton.frame = CGRectMake(210.0, 65.0, 90, 25);
[registerButton setImage:[UIImage imageNamed:@"register.png"] forState:UIControlStateNormal];
[registerButton addTarget:self action:@selector(registerButtonAction) forControlEvents:UIControlEventTouchUpInside];
[cellimg addsubView:registerButton];