开发者

Create button inside tableview cell without Interface Builder

开发者 https://www.devze.com 2023-02-19 03:14 出处:网络
I try to create a button inside the tableview cell without using Interface Builder. I found this code that I thought it is for creating a button:

I try to create a button inside the tableview cell without using Interface Builder. I found this code that I thought it is for creating a button:

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.开发者_如何学Pythonframe = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];

}

but when I try to run the program nothing show up in the simulator. am I missing something to create the button with this code?


You need to add the button to the cell - have you tried

[cell.contentView addSubview:button]

?

Adjust your button's frame to position it within the contentView and to size the button correctly.


You should write your all code in cellforRow At index Datasource method and for add your button on cell you can use this line of code

[cell.contentView addsubview:buttonobject];

button.frame = CGRectMake(10,10,30,30);

And also change Tour button background or style for your conformation it will be there or not.

It will surly work if its working inform us.

Your Welcome.


You have to add code in this method but first of all add custom cell to your table view,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 forwordBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [forwordBtn setTitle:@"Forward" forState:UIControlStateNormal];

    [forwordBtn addTarget:self 
                   action:@selector(forwordButtonPressed)
         forControlEvents:UIControlEventTouchUpInside];
    forwordBtn.frame = CGRectMake(0, 0, 10, 10);

    [cell.contentview addSubview:forwordBtn];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号