Reputation: 57
I am working on an application I want to add an imagebutton in tableviewcell and tableviewcell have already an Image(Gradient image with dimension 320 * 103) and I want to add an imagebutton on cell. we are adding but button are not display.
My code is:
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];
Upvotes: 0
Views: 822
Reputation: 10333
Fixed this for you. Not only were you reallocating button, but also you leaked 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];
Upvotes: 1