【问题标题】:How to create a Button action inside custom table cell in ios如何在ios中的自定义表格单元格内创建按钮操作
【发布时间】:2014-06-27 04:58:52
【问题描述】:

我正在使用自定义的tablecell,我想要在cell 中使用两个按钮操作,一个用于pushViewController,一个用于popViewControllerAnimated 如何实现?

【问题讨论】:

  • [cell.btnObject addTarget:self action:@selector(pushME) forControlEvents:UIControlEventTouchUpInside]; 实现 pushMe 方法。流行音乐也是如此。
  • 感谢您的帖子,您能否详细说明并发布答案,这会有所帮助
  • 您能否展示如何创建自定义单元格,以便我知道并帮助您。是的,实现该方法并在该方法中[self.navigationController popViewControllerAnimated:YES]; for pop。
  • cell.back.tag = indexPath.row; [cell.back addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];.我正在使用它,但是如何将按钮操作放在方法中?

标签: ios iphone xcode uitableview


【解决方案1】:

在您的自定义单元格中,您必须创建两个按钮。在您的cellForRowAtIndexPath 中写入以下代码:

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

{
     CellCustomCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"customCell"];

     cell.btnPop.tag = indexPath.row; 
     [cell.btnPop addTarget:self action:@selector(popButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

     cell.btnPush.tag = indexPath.row; 
     [cell.btnPush addTarget:self action:@selector(pushButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

      return cell;
}

为按钮设置标签很重要,因为它们会告诉您点击了哪个行按钮。

定义你的行动:

-(void)popButtonClicked:(id)sender
{

}

-(void)pushButtonClicked:(id)sender
{

}

【讨论】:

    【解决方案2】:

    假设您在 CellForRowIndex 的一侧设置如下:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
            CellInviteTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"CellInviteTableViewCell"];
    
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.back.tag = indexPath.row; 
           [cell.back addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
     return cell;
    
    }
    
    
    -(void)yourButtonClicked:(UIButton*)sender
    {
         NSLog(@"button tapped Index %d",sender.tag);
        //here you get its each button action you can identirire which button click by its tag
    
    }
    

    【讨论】:

      【解决方案3】:
      In cellForRowAtIndexPath :
      
      
      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      
      {
      
      static NSString *SimpleTableIdentifier = @"myTableViewCell";
      
      myTableViewCell *cell=(myTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
      
      [cell clearsContextBeforeDrawing];
      
      if (cell==nil)
       {
              NSArray *cellObjects=[[NSBundle mainBundle]loadNibNamed:@"myTableViewCell" owner:self options:nil];
      
              for (id currentObject in cellObjects)
              {
              if ([currentObject isKindOfClass:[UITableViewCell class]])
                      {
                          cell=(myTableViewCell *)currentObject;
                      }
                  }
      }
      
      [cell.btnPlus setTag:indexPath.row];
      
      [cell.btnPlus addTarget:self action:@selector(btnPlus_Click:) forControlEvents:UIControlEventTouchUpInside];
      
      return cell;
      
      }
      
      
      
      -(IBAction)btnPlus_Click:(id)sender
      {
      
      }
      

      【讨论】:

        【解决方案4】:

        在你的 cellForRowAtIndexPath 中像这样创建

        cell.deleteBtn.tag = indexPath.row;
        [cell.deleteBtn addTarget:self action:@selector(cellDeleteAction:) forControlEvents:UIControlEventTouchUpInside];
        

        然后创建按钮操作

        -(void)cellDeleteAction:(UIButton *)sender {
            UIButton *button = (UIButton *)sender;
            NSString *rateId = [rate_IdArray objectAtIndex:button.tag];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-25
          • 1970-01-01
          • 2012-06-08
          • 2016-02-02
          • 1970-01-01
          相关资源
          最近更新 更多