iOS - 无法从UITableViewCell中动态创建的按钮调用操作

时间:2022-10-22 07:44:34

i have created two button programmatically in UITableView
i.e Edit Delete

我在UITableView中以编程方式创建了两个按钮,即编辑删除

when we click the cell these buttons are displayed but when i try to click in edit or delete button it doesn't calls the appropriate method i.e edit or deleteBtn.
This is my code.

当我们单击单元格时会显示这些按钮,但是当我尝试单击编辑或删除按钮时,它不会调用适当的方法,即edit或deleteBtn。这是我的代码。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"Cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell==nil){
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
    }

    NSString *cellValue=[firstName objectAtIndex:indexPath.row];
    cell.textLabel.text=cellValue;


    edit=[[UIButton alloc]init];
    [edit setTitle:@"Edit" forState:UIControlStateNormal];
    [edit setFrame:CGRectMake(100, 100, 100, 20)];
    [edit setTag:1];
    [edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

    delete=[[UIButton alloc]init];
    [delete setTitle:@"Delete" forState:UIControlStateNormal];
    [delete setFrame:CGRectMake(150, 100, 100, 20)];
    [delete setTag:2];
    [delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:delete];
    [cell.contentView addSubview:edit];

    return cell;

}

my edit and delete function are very simple

我的编辑和删除功能非常简单

-(void)edit{

NSLog("%@",selectedValue);
}

-(void)deleteBtn{

NSLog("%@",selectedValue);
}

i have checked this function applying breakpoint but it is not called.

我已经检查了这个函数应用断点但它没有被调用。

this is how my selectedValue comes

这就是我的selectedValue的来源

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
selectedValue=[firstName objectAtInder:indexPath.row];
}

Thanks, in advance,
Arun.

谢谢,提前,阿伦。

10 个解决方案

#1


2  

Think,the problem is with UIButton frame's. in your code both button's origin.y is 100.0,so it went beyond the cell's bound (Default height is 44.0).

想想,问题在于UIButton框架。在你的代码中,两个按钮的origin.y都是100.0,所以它超出了单元格的界限(默认高度为44.0)。

Change your Both UIButton frame like this,and it is worked for me.

像这样更改你的两个UIButton框架,它对我有用。

UIButton *  edit =[[UIButton alloc]init];
[edit setBackgroundColor:[UIColor blackColor]];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
**[edit setFrame:CGRectMake(100,5.0,100, 30)];**
[edit setTag:1];
[edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

UIButton*  delete=[[UIButton alloc]init];
[delete setBackgroundColor:[UIColor blackColor]];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
**[delete setFrame:CGRectMake(200,5.0,100,30)];**
[delete setTag:2];
[delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];

#2


2  

please your code like below

请你的代码如下

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell==nil){
    cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}

NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;


edit=[[UIButton alloc]init];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 5, 100, 20)];
[edit setTag:indexPath.row];
[edit addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:edit];   

delete=[[UIButton alloc]init];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(210, 5, 100, 20)];
[delete setTag:indexPath.row];
[delete addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:delete];

return cell;

}

use methods like below

使用如下方法

-(IBAction)edit:(id)sender
{
    int tag = [sender tag];
    NSString *str = [firstName objectAtIndex:tag];
    NSLog("%@",str);
 }

 -(IBAction)deleteBtn:(id)sender
 {
    int tag = [sender tag];
    NSString *str = [firstName objectAtIndex:tag];
    NSLog("%@",str);
 }

#3


1  

Your buttons are being added over and over to the cell because they're being created every time you call CFRAIP. Move them into the cell == nil block:

您的按钮会一次又一次地添加到单元格中,因为每次调用CFRAIP时都会创建它们。将它们移动到单元格== nil块:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"Cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell==nil){
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        edit=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
        [edit setTitle:@"Edit" forState:UIControlStateNormal];
        [edit setFrame:CGRectMake(100, 100, 100, 20)];
        [edit setTag:1];
        [edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

        delete=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
        [delete setTitle:@"Delete" forState:UIControlStateNormal];
        [delete setFrame:CGRectMake(150, 100, 100, 20)];
        [delete setTag:2];
        [delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:delete];
        [cell.contentView addSubview:edit];
    }

    NSString *cellValue=[firstName objectAtIndex:indexPath.row];
    cell.textLabel.text=cellValue;
    return cell;

}

#4


1  

The problem is in the setFrame

问题出在setFrame中

The height of the table cell is less than the co-ordinates you have set for the buttons. Change the code to

表格单元格的高度小于您为按钮设置的坐标。将代码更改为

[edit setFrame:CGRectMake(100, 5, 100, 20)]; 

and this will work.

这将有效。

OR

just increase the height of the table rows, which by default is 44, using

只需增加表行的高度,默认情况下为44,使用

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

#5


0  

Please try to use this one ....and do same thing for delete button ..........

请尝试使用这个....并为删除按钮做同样的事情..........

if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        // -------------------- Add button to cell --------------
        edit = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 20)];

        [edit setTitle:@"Edit" forState:UIControlStateNormal];
        [edit setTag:1];
        [edit addTarget:self action:@selector(edit)   forControlEvents:UIControlEventTouchUpInside];        

        [cell.contentView addSubview:edit];
    }

#6


0  

I can you try to custom cell and can you add buttons in custom cell view and implement below codes.

我可以尝试自定义单元格,您可以在自定义单元格视图中添加按钮并实现下面的代码。

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

    static NSString *CellIdentifier = @"Cell";

    cell = [self.tbl dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {


        //****cell recent deal buy button click goes to login or check out pageview 
        //[cell.btn setTag:indexPath.row];
        //[cell.btn addTarget:self action:@selector(buybutton_checkout:)
       //   forControlEvents:UIControlEventTouchDown];
       // cell.btn.hidden=NO;


        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"icel" owner:nil options:nil];

        for (UIView *view in views)
        {
            if([view isKindOfClass:[UITableViewCell class]])
            {

                cell = (icel*)view;

            }

        }
    }

   //tbl.layer.cornerRadius = 3.9;
    //[tbl setClipsToBounds:YES];


    [cell.activity startAnimating]; 

    [cell.btn setTitle:_BUYNOW_BTN forState:UIControlStateNormal];
    [cell.btn setTag:indexPath.row];
    [cell.btn addTarget:self action:@selector(Buy_btnlck:)
                forControlEvents:UIControlEventTouchDown];
        //cell.btn.hidden=NO;
    cell.title_lbl.text=[[_today_similardeal_title_ary objectAtIndex:indexPath.row]capitalizedString];


        index_tbl=indexPath.row;
    return cell;
    }
}
-(IBAction)Buy_btnlck:(UIButton *)button
{
NSInteger intvalue=[[NSString stringWithFormat:@"%ld",(long int)[button tag]]intValue];
}

#7


0  

Don't use [UIButton alloc] init]. Use instead this

不要使用[UIButton alloc] init]。请改用此

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(edit:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Edit" forState:UIControlStateNormal];
 button.frame = CGRectMake(80.0, 21.0, 40.0, 40.0);
[view addSubview:button];

Where view is the view where you are adding your button as a subview. you can also set Button frame according to your requirements.

视图是将按钮添加为子视图的视图。您也可以根据您的要求设置Button框架。

#8


0  

Pls change the button names , as Edit and delete are already defined keywords in Objective C

请更改按钮名称,因为编辑和删除已在Objective C中定义了关键字

#9


0  

Modify your code as like this....

像这样修改你的代码....

Replace the below two lines...

更换以下两行......

edit=[[UIButton alloc] init];
delete=[[UIButton alloc] init];

as like this...

像这样......

UIButton *edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIButton *delete=[UIButton buttonWithType:UIButtonTypeRoundedRect]];

#10


0  

Try this::

Table Method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Configure the cell.
   UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if(cell==nil){
      cell= [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];

      NSString *cellValue=[firstName objectAtIndex:indexPath.row];
      cell.textLabel.text=cellValue;

      UIButton *btnEdit=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnEdit setTitle:@"Edit" forState:UIControlStateNormal];
      [btnEdit setFrame:CGRectMake(100, 100, 100, 20)];
      [btnEdit setTag:indexPath.row];
      [btnEdit addTarget:self action:@selector(clickEdit:) forControlEvents:UIControlEventTouchUpInside];

      [cell.contentView addSubview:btnEdit];


      UIButton *btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnDelete setTitle:@"Delete" forState:UIControlStateNormal];
      [btnDelete setFrame:CGRectMake(150, 100, 100, 20)];
      [btnDelete setTag:indexPath.row];
      [btnDelete addTarget:self action:@selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];

      [cell.contentView addSubview:btnDelete];
   }
   return cell;
}

Then, Button Action Methods

然后,按钮操作方法

-(IBAction)clickEdit:(id)sender
{
   NSString *str = [firstName objectAtIndex:[sender tag]];
   NSLog("Name :: %@",str);
}

-(IBAction)clickDelete:(id)sender
{
   NSString *str = [firstName objectAtIndex:[sender tag]];
   NSLog("Name :: %@",str);
}

Hopefully, it'll help you.

希望它会对你有所帮助。

Thanks.

#1


2  

Think,the problem is with UIButton frame's. in your code both button's origin.y is 100.0,so it went beyond the cell's bound (Default height is 44.0).

想想,问题在于UIButton框架。在你的代码中,两个按钮的origin.y都是100.0,所以它超出了单元格的界限(默认高度为44.0)。

Change your Both UIButton frame like this,and it is worked for me.

像这样更改你的两个UIButton框架,它对我有用。

UIButton *  edit =[[UIButton alloc]init];
[edit setBackgroundColor:[UIColor blackColor]];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
**[edit setFrame:CGRectMake(100,5.0,100, 30)];**
[edit setTag:1];
[edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

UIButton*  delete=[[UIButton alloc]init];
[delete setBackgroundColor:[UIColor blackColor]];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
**[delete setFrame:CGRectMake(200,5.0,100,30)];**
[delete setTag:2];
[delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:delete];
[cell.contentView addSubview:edit];

#2


2  

please your code like below

请你的代码如下

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell==nil){
    cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}

NSString *cellValue=[firstName objectAtIndex:indexPath.row];
cell.textLabel.text=cellValue;


edit=[[UIButton alloc]init];
[edit setTitle:@"Edit" forState:UIControlStateNormal];
[edit setFrame:CGRectMake(100, 5, 100, 20)];
[edit setTag:indexPath.row];
[edit addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:edit];   

delete=[[UIButton alloc]init];
[delete setTitle:@"Delete" forState:UIControlStateNormal];
[delete setFrame:CGRectMake(210, 5, 100, 20)];
[delete setTag:indexPath.row];
[delete addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:delete];

return cell;

}

use methods like below

使用如下方法

-(IBAction)edit:(id)sender
{
    int tag = [sender tag];
    NSString *str = [firstName objectAtIndex:tag];
    NSLog("%@",str);
 }

 -(IBAction)deleteBtn:(id)sender
 {
    int tag = [sender tag];
    NSString *str = [firstName objectAtIndex:tag];
    NSLog("%@",str);
 }

#3


1  

Your buttons are being added over and over to the cell because they're being created every time you call CFRAIP. Move them into the cell == nil block:

您的按钮会一次又一次地添加到单元格中,因为每次调用CFRAIP时都会创建它们。将它们移动到单元格== nil块:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"Cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell==nil){
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        edit=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
        [edit setTitle:@"Edit" forState:UIControlStateNormal];
        [edit setFrame:CGRectMake(100, 100, 100, 20)];
        [edit setTag:1];
        [edit addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

        delete=[UIButton buttonWithType:UIButtonTypeRoundedRect];;
        [delete setTitle:@"Delete" forState:UIControlStateNormal];
        [delete setFrame:CGRectMake(150, 100, 100, 20)];
        [delete setTag:2];
        [delete addTarget:self action:@selector(deleteBtn) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView addSubview:delete];
        [cell.contentView addSubview:edit];
    }

    NSString *cellValue=[firstName objectAtIndex:indexPath.row];
    cell.textLabel.text=cellValue;
    return cell;

}

#4


1  

The problem is in the setFrame

问题出在setFrame中

The height of the table cell is less than the co-ordinates you have set for the buttons. Change the code to

表格单元格的高度小于您为按钮设置的坐标。将代码更改为

[edit setFrame:CGRectMake(100, 5, 100, 20)]; 

and this will work.

这将有效。

OR

just increase the height of the table rows, which by default is 44, using

只需增加表行的高度,默认情况下为44,使用

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

#5


0  

Please try to use this one ....and do same thing for delete button ..........

请尝试使用这个....并为删除按钮做同样的事情..........

if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        // -------------------- Add button to cell --------------
        edit = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 20)];

        [edit setTitle:@"Edit" forState:UIControlStateNormal];
        [edit setTag:1];
        [edit addTarget:self action:@selector(edit)   forControlEvents:UIControlEventTouchUpInside];        

        [cell.contentView addSubview:edit];
    }

#6


0  

I can you try to custom cell and can you add buttons in custom cell view and implement below codes.

我可以尝试自定义单元格,您可以在自定义单元格视图中添加按钮并实现下面的代码。

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

    static NSString *CellIdentifier = @"Cell";

    cell = [self.tbl dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {


        //****cell recent deal buy button click goes to login or check out pageview 
        //[cell.btn setTag:indexPath.row];
        //[cell.btn addTarget:self action:@selector(buybutton_checkout:)
       //   forControlEvents:UIControlEventTouchDown];
       // cell.btn.hidden=NO;


        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"icel" owner:nil options:nil];

        for (UIView *view in views)
        {
            if([view isKindOfClass:[UITableViewCell class]])
            {

                cell = (icel*)view;

            }

        }
    }

   //tbl.layer.cornerRadius = 3.9;
    //[tbl setClipsToBounds:YES];


    [cell.activity startAnimating]; 

    [cell.btn setTitle:_BUYNOW_BTN forState:UIControlStateNormal];
    [cell.btn setTag:indexPath.row];
    [cell.btn addTarget:self action:@selector(Buy_btnlck:)
                forControlEvents:UIControlEventTouchDown];
        //cell.btn.hidden=NO;
    cell.title_lbl.text=[[_today_similardeal_title_ary objectAtIndex:indexPath.row]capitalizedString];


        index_tbl=indexPath.row;
    return cell;
    }
}
-(IBAction)Buy_btnlck:(UIButton *)button
{
NSInteger intvalue=[[NSString stringWithFormat:@"%ld",(long int)[button tag]]intValue];
}

#7


0  

Don't use [UIButton alloc] init]. Use instead this

不要使用[UIButton alloc] init]。请改用此

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(edit:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Edit" forState:UIControlStateNormal];
 button.frame = CGRectMake(80.0, 21.0, 40.0, 40.0);
[view addSubview:button];

Where view is the view where you are adding your button as a subview. you can also set Button frame according to your requirements.

视图是将按钮添加为子视图的视图。您也可以根据您的要求设置Button框架。

#8


0  

Pls change the button names , as Edit and delete are already defined keywords in Objective C

请更改按钮名称,因为编辑和删除已在Objective C中定义了关键字

#9


0  

Modify your code as like this....

像这样修改你的代码....

Replace the below two lines...

更换以下两行......

edit=[[UIButton alloc] init];
delete=[[UIButton alloc] init];

as like this...

像这样......

UIButton *edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIButton *delete=[UIButton buttonWithType:UIButtonTypeRoundedRect]];

#10


0  

Try this::

Table Method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Configure the cell.
   UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if(cell==nil){
      cell= [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];

      NSString *cellValue=[firstName objectAtIndex:indexPath.row];
      cell.textLabel.text=cellValue;

      UIButton *btnEdit=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnEdit setTitle:@"Edit" forState:UIControlStateNormal];
      [btnEdit setFrame:CGRectMake(100, 100, 100, 20)];
      [btnEdit setTag:indexPath.row];
      [btnEdit addTarget:self action:@selector(clickEdit:) forControlEvents:UIControlEventTouchUpInside];

      [cell.contentView addSubview:btnEdit];


      UIButton *btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
      [btnDelete setTitle:@"Delete" forState:UIControlStateNormal];
      [btnDelete setFrame:CGRectMake(150, 100, 100, 20)];
      [btnDelete setTag:indexPath.row];
      [btnDelete addTarget:self action:@selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];

      [cell.contentView addSubview:btnDelete];
   }
   return cell;
}

Then, Button Action Methods

然后,按钮操作方法

-(IBAction)clickEdit:(id)sender
{
   NSString *str = [firstName objectAtIndex:[sender tag]];
   NSLog("Name :: %@",str);
}

-(IBAction)clickDelete:(id)sender
{
   NSString *str = [firstName objectAtIndex:[sender tag]];
   NSLog("Name :: %@",str);
}

Hopefully, it'll help you.

希望它会对你有所帮助。

Thanks.