Safari
Safari

Reputation: 11935

add a button in a tableview cell iphone

I have this problem ... I want to put my button in a cell of my table view .. I do this ..

UIButton *dowMapBt = [UIButton buttonWithType:UIButtonTypeCustom];
        [dowMapBt setFrame:CGRectMake(0, 0, cell.frame.size.width, 70)];
        [dowMapBt setTitle:@"Download Map" forState:UIControlStateNormal];

        dowMapBt.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];

        [dowMapBt setBackgroundImage:[[UIImage imageNamed:@"00670_btn_green_medium.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
        [dowMapBt addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:dowMapBt];

but I have a problem .. the button does not appear in all cell, but only in a half .. and not for the entire width of the cell ...

Upvotes: 0

Views: 837

Answers (2)

user9930
user9930

Reputation: 147

Just Add button as a subview of cell after creating cell.First check if cell is nil or not.

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [dowMapBt setFrame:CGRectMake(0, 0, cell.frame.size.width, 30)];
    [dowMapBt addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:dowMapBt];
}

Ater this return cell

Upvotes: 0

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

Set frame like this

  [dowMapBt setFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];

And also check the size of the image you are setting

Upvotes: 1

Related Questions