Add Button in UITableViewCell

I want to add a button to UITableViewCell. This is my code: `

if (indexPath.row==2) {
    UIButton *scanQRCodeButton = [[UIButton alloc]init];

    scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
    scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    scanQRCodeButton.backgroundColor = [UIColor redColor];
    [scanQRCodeButton setTitle:@"Hello" forState:UIControlStateNormal];

    [cell addSubview:scanQRCodeButton];
}`

Now when I launch the application, I see only an empty line! Any ideas?

+5
source share
5 answers

While it is natural to put it in the contentView of the cell, I am pretty sure that this is not a problem (in fact, in the past I never had subviews view correctly in the contentView, so I always used the cell).

In any case, the problem is with the first three lines when you start creating your button. The first two lines are fine, but the code stops working with:

scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

buttonWithType: ( alloc-init). " " ( ). init buttonWithType: , .

UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:@"Hello" forState:UIControlStateNormal];    
[cell addSubview:scanQRCodeButton];

( , cell.contentView, ). Automatic Reference Counting (ARC), , , buttonWithType: .

+13
    UIButton *deletebtn=[[UIButton alloc]init];
            deletebtn.frame=CGRectMake(50, 10, 20, 20);
            deletebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [deletebtn setImage:[UIImage imageNamed:@"log_delete_touch.png"] forState:UIControlStateNormal];
            [deletebtn addTarget:self action:@selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:deletebtn];

// UIButton + EventBlocks

UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setFrame:CGRectMake(170,5, 25, 25)];
deletebtn.tag=indexPath.row;
[deletebtn setImage:[UIImage imageNamed:@"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn setOnTouchUpInside:^(id sender, UIEvent *event) {


  //Your action here
}];
[cell addSubview:deletebtn];
+7

- contentView.

, [cell addSubview:scanQRCodeButton];
do [cell.contentView addSubview:scanQRCodeButton];

+4

[cell.contentView addSubview:scanQRCodeButton];, , , textLabel . , , accesoryView cell.accesoryView = scanQRCodeButton;.

+1

setTitle ,

[UIButton.titleLabel setText:@""]

setTitle.

:

[scanQRCodeButton.titleLabel setText:@"Hello"];

.

0

All Articles