【问题标题】:UITableView section clicked in Viewforheaderinsection在 Viewforheaderinsection 中单击的 UITableView 部分
【发布时间】:2024-01-16 03:36:01
【问题描述】:

我在UITableView viewForHeaderInSection 中创建了自定义UIButton。现在,当我单击该部分中的按钮时,我如何知道单击了哪个部分?请帮忙。

【问题讨论】:

  • 按钮是静态的还是动态的?

标签: iphone uitableview uibutton


【解决方案1】:

当您在 viewForHeaderInSection 添加按钮时,只需设置带有节号的标签,如下所示。,..

button.tag = section;

当您点击按钮时,您可以使用以下代码获取该数字...

- (IBAction)yourButton_Clicked:(id) sender  
{
    NSLog(@"Section Number => %d",[sender tag]);
}

【讨论】:

    【解决方案2】:
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
    UIbutton *btn =[[UIButton alloc]init];
    btn.tag=section;// this tag property will differentiate button for different section
    }
    

    通过这个,您可以访问 btn 并在该 burron 上添加事件

    【讨论】:

      【解决方案3】:
      -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
      {
          UIbutton *button =[[UIButton alloc]init];
          button.tag=section;// this tag property will differentiate button for different section
      }
      

      【讨论】:

        【解决方案4】:

        在 didselectrowatindexpath 检查 if(index path.section) 是否等于您想要操作的部分。

        【讨论】:

        • OP 讨论的是表格单元格中的自定义 UIButton,而不是表格单元格本身
        • 您可以检查该部分,因为您可以更改单元格..在评论兄弟之前尝试一下..
        • 每个部分都有一个 UIButton,当我点击按钮时,我需要知道点击了哪个部分,以便我只能展开该部分的行
        最近更新 更多