Add button to UITableViewCell

前端 未结 5 1650
一个人的身影
一个人的身影 2021-02-03 15:07

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

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

           


        
相关标签:
5条回答
  • 2021-02-03 15:48

    While it's natural to put it in the contentView of the cell, I'm fairly certain that is not the problem (actually, in the past, I've never had subviews displayed correctly in the contentView, so I've always used the cell).

    Anyway, the problem involves the first three lines of when you start creating your button. The first two lines are fine, but the code stops working with:

    scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    

    buttonWithType: is actually a convenience method to create a button (it's like a compact alloc-init). Therefore, it actually "nullifies" your past two lines (you basically created the button twice). You can only use either init or buttonWithType: for the same button, but not both.

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

    This will work (note that you can use cell.contentView if you wanted). In case you're not using Automatic Reference Counting (ARC), I would like to mention that you don't have to do anything in term of memory management, because buttonWithType: returns an autoreleased button.

    0 讨论(0)
  • 2021-02-03 15:48

    You want to add any custom UI elements to the cell's contentView.

    So, instead of [cell addSubview:scanQRCodeButton];
    do [cell.contentView addSubview:scanQRCodeButton];

    0 讨论(0)
  • 2021-02-03 15:49

    Try adding [cell.contentView addSubview:scanQRCodeButton]; or if you want the button to the left side look at my question at the answer, to move the textLabel to the side. If you want the button to the right then just set it as your accesoryView like this cell.accesoryView = scanQRCodeButton;.

    0 讨论(0)
  • 2021-02-03 15:54
        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];
    

    or

    // Download class and import in your project 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];
    
    0 讨论(0)
  • 2021-02-03 16:09

    Method setTitle does not work in my code, so I have set by using

    [UIButton.titleLabel setText:@""]
    

    instead of using setTitle method.

    Please try again with following code:

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

    Then it would work well.

    0 讨论(0)
提交回复
热议问题