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

Posted

技术标签:

【中文标题】iOS - 无法从 UITableViewCell 中动态创建的按钮调用操作【英文标题】:iOS - Unable to call action from dynamically created button in UITableViewCell 【发布时间】:2013-03-18 05:53:13 【问题描述】:

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

当我们单击单元格时,会显示这些按钮,但是当我尝试单击 edit 或 delete 按钮时,它不会调用适当的方法,即 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;


我的编辑和删除功能很简单

-(void)edit

NSLog("%@",selectedValue);


-(void)deleteBtn

NSLog("%@",selectedValue);

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

我的 selectedValue 就是这样来的

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

谢谢,提前, 阿伦。

【问题讨论】:

发布您的 edit 和 deleteBtn 方法。 编辑和删除方法是什么样的? 你会粘贴edit代码还是deleteBtn? 我创建了这个函数来调用网络服务来删除或编辑选定的记录 你放“UITableViewDatasourceDelegate”和“UITableViewDelegate”了吗?在 .h 文件中 【参考方案1】:

想想,问题出在UIButton 框架上。 在您的代码中,两个按钮的 origin.y 都是 100.0,因此它超出了单元格的边界(默认高度为 44.0)。

像这样更改您的 Both 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];

【讨论】:

它工作了,但是编辑按钮会自动显示在所有单元格中,我希望它在选择单元格时显示,当我选择任何行时会出现删除按钮。 这个方法可以在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中添加删除按钮。【参考方案2】:

请输入如下代码

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

使用以下方法

-(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】:

您的按钮被一遍又一遍地添加到单元格中,因为每次调用 CFRAIP 时都会创建它们。将它们移动到cell == 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;


【讨论】:

我同意@mohan,更改您的按钮声明。我已经编辑了我的答案。【参考方案4】:

问题出在setFrame

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

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

这会奏效。

只需增加表格行的height,默认为44,使用

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

【讨论】:

【参考方案5】:

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

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】:

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

 - (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】:

不要使用[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];

其中视图是将按钮添加为子视图的视图。也可以根据自己的需求设置Button frame。

【讨论】:

【参考方案8】:

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

【讨论】:

【参考方案9】:

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

替换下面两行...

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

像这样……

UIButton *edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];

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

【讨论】:

【参考方案10】:

试试这个::

表格法

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

然后,按钮操作方法

-(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);

希望对你有所帮助。

谢谢。

【讨论】:

以上是关于iOS - 无法从 UITableViewCell 中动态创建的按钮调用操作的主要内容,如果未能解决你的问题,请参考以下文章

无法在 IOS 6 中将 UILabel 添加到 UITableViewCell

在 iOS 中使用自定义单元格时无法查看 UITableViewCell

无法再与 iOS 14 中的 UICollectionViewCell 或 UITableViewCell 中的内容进行交互 [重复]

添加到 UITableViewCell 的 contentView 的 UITextField 在 Popover 中无法正确显示(iPad、iOS 4.2)

如何从自定义 UITableViewCell 触发 segue - iOS7

无法从 UITableViewCell 调用 performeguewithidentifier