简体   繁体   English

在tableview手机iphone中添加按钮

[英]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 ... 但是我有一个问题..按钮不会出现在所有单元格中,而是仅出现在一半中,而不是整个单元格的宽度...

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 还要检查您设置的图像尺寸

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 在此返回单元格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM