Q:

UITableView and UIButton overlap without taking Cell Height into consideration

I have a button within a tableView that doesn't behave as I would like.
I've placed my button initialisation within my cell==nil block in the tableView cellForRowAtIndexPath. When I move it out of this block the button is drawn multiple times with all the other buttons on top so I'm unable to move it (unless this is set up wrong).
My button is a simple green tick or red cross and is set up with a selector. Each row height within the tableView differs depending on the amount of text within it. My button however, when in the cell==nil part, doesn't take into consideration cell.frame.height. It reuses the height of the cells from the first time the tableView is initialised.
This is the code for the button:
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button =[[UIButton alloc]init];

    float framePos_x = cell.frame.size.width/1.30;
    float framePos_y = cell.frame.size.height - 55;

    NSLog(@"%f",cell.frame.size.height);

    button.frame = CGRectMake(framePos_x, framePos_y, 35, 35);

    buttonImage = [UIImage imageNamed:@"Notification_Accepted"];
    buttonImage_Pressed = [UIImage imageNamed:@"Notification_Reject"];

    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setImage:buttonImage_Pressed  forState:UIControlStateSelected];

    button.tag = [UserID intValue];
    [button addTarget:self action:@selector(ResponseFlagButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

The output of my NSLog is: 
2014-08-03 17:03:49.724 TableApp[5540:60b] 120.000000
2014-08-03 17:03:49.774 TableApp[5540:60b] 100.000000
2014-08-03 17:03:49.853 TableApp[5540:60b] 100.000000
2014-08-03 17:03:49.936 TableApp[5540:60b] 100.000000
2014-08-03 17:03:51.287 TableApp[5540:60b] 100.000000

When this log is within the cell=nil block that's all it outputs, even when scrolling up and down. Out of the cell=nil block and it works fine - same as the button (except for it's redraw issue).
How would I get the button to be drawn once each time without it being place on top of the old button. As well as taking into consideration the variable cell height?

A:

I would do a combination of both Michal's and Lev's answers.

Subclass UITableViewCell and make the button a property.
Instantiate the button during the cell's own instantiation.  This way you will only be adding the button once.
Override the prepareForReuse method of your UITableViewCell subclass and change only the appearance of the button as needed.
Override the layoutSubviews method of your UITableViewCell subclass and calculate and set the new height for the cell.  (You are implementing your own custom intrinsic size here. Cool!)

