简体   繁体   中英

How to add the button in uitableviewcell in ipad

I am new in ipad development i want to add the button in uitableviewcell but it is not displaying. how to add this button in tableview cell. I am writing this code in cellforRowAtIndexPath My code is:

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];

Thanks in Advance:

The problem is in your button's frame. Try this :

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];

Try this code. You mentioned the 'Y axis' is 350. Please check you row height. And give the 'Y' axis related to your row height.

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];

1)check Table cell height

2)Check frame size of button (means x and y axies)

You can just create a prototype cell in interface builder and drag a UIButton in the cell. after that create a new UITableViewCell class and link the button.

don't forget to add a identifier to the cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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