views:

343

answers:

1

I have an UIScrollView where I create two buttons. The first button works as it should, however the second button disappears. It is still there and can accept clicks, and becomes visible when you click on the hidden button.

Any idead on why the second button dis appears?

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


button_1        = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

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

[button_1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button_1 setTitle:@"Circle" forState:UIControlStateNormal];            
button_1.frame = CGRectMake(0.0, 30.00, 50, 20);    
[button_1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button_1];


button_2        = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

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

[button_2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button_2 setTitle:@"Square" forState:UIControlStateNormal];            
button_2.frame = CGRectMake(0.0, 120.0, 50, 20);    
[button_2 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button_2];
+2  A: 

What is the layout you're trying to achieve here? Table cells are, by default (unless you have custom rowHeights) 44px high. Your button_2 is going to be way out of the cell's bounds, far below the first button. Also, there's no reason to, and in fact you should not, retain the buttons you're creating. They'll be retained when you add them to their parent view.

Ben Gottlieb
@Ben - Thanks for the answer. Yes, we are using calculatedCell heights for each cell so the heights are different for each cell. I need to do a calculate cell height before the cell is draw, with multiple buttons in the table cell. However, the location of the buttons will change in each cell.
Wenzi
Is the second button disappearing when the first button is hit, or is it starting out hidden?
Ben Gottlieb
@Ben - Starting out hidden. I had fooled around with 'setHidden = No' just to make sure, but it did not help. Also if you clicked the button, you could see it when it was clicked. I also tried UIControlStateHighlighted for the state but that did not help either.
Wenzi
I would try stripping the button down to as little code as possible, and moving it right next to the first button. If that works, start adding back the original code and see where it stops working.
Ben Gottlieb
@Ben - Thanks for the help.
Wenzi