如何在ipad中添加uitableviewcell中的按钮

时间:2012-01-25 08:23:45

标签: iphone objective-c ipad

我是ipad开发的新手我想在uitableviewcell中添加按钮,但它没有显示。如何在tableview单元格中添加此按钮。我在cellforRowAtIndexPath中编写此代码我的代码是:

UIButton *cellImgButton = [[UIButton alloc]initWithFrame:CGRectMake(300, 350, 40, 40)];
cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"remove.png"];
[cellImgButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cellImgButton addTarget:self action:@selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView  addSubview:cellImgButton];

提前致谢:

4 个答案:

答案 0 :(得分:1)

问题出在按钮的框架中。试试这个:

UIButton *cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellImgButton setFrame:CGRectMake(0, 5, 40 , 40) ];

UIImage *buttonImage = [UIImage imageNamed:@"remove.png"];
[cellImgButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cellImgButton addTarget:self action:@selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView  addSubview:cellImgButton];

答案 1 :(得分:1)

试试这段代码。你提到'Y轴'是350.请检查行高。并给出与行高相关的'Y'轴。

UIButton *cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellImgButton setFrame:CGRectMake(10, 5, 40 , 40) ];
[cellImgButton setBackgroundImage:[UIImage imageNamed:@"remove.png"] forState:UIControlStateNormal];
[cellImgButton addTarget:self action:@selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellImgButton];

答案 2 :(得分:0)

1)检查表格单元格高度

2)检查按钮的框架尺寸(表示x和y轴)

答案 3 :(得分:0)

您可以在界面构建器中创建原型单元格,然后在单元格中拖动UIButton。之后,创建一个新的UITableViewCell类并链接按钮。

不要忘记在单元格中添加标识符