【问题标题】:Figure out section of a section header when tapped in a UITableView在 UITableView 中点击时找出部分标题的部分
【发布时间】:2011-12-12 03:52:19
【问题描述】:

我有一个带有节标题自定义视图的 UITableView。我在客户部分标题视图中添加了一个 UITapGestureRecognizer 以检测何时有人点击了部分标题。

如何确定节标题属于哪个节?

提前致谢。

【问题讨论】:

    标签: iphone uiview uitableview


    【解决方案1】:

    最简单的方法是在节标题视图类上指定一个属性来保存节索引,然后将索引分配给-tableView:viewForHeaderInSection: 中的该属性,如下所示:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        // CustomHeaderView *headerView = ...
    
        headerView.section = section;
    
        // ...
        return headerView;
    }
    

    然后让手势回调查看该属性。

    【讨论】:

    • 谢谢,这是我尝试过的。但是,当我在表格中间插入一个新节时,所有节号都不同步。有什么技巧可以克服这个问题?
    • 嗯,在这种情况下,看看添加部分后调用-reloadSections:withRowAnimation:是否有帮助。
    【解决方案2】:

    您提供的action 方法必须具有following signature:

    - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;
    

    gestureRecognizer 具有以下属性:

    获取识别器的状态和视图
    国有财产
    查看房产
    启用属性

    所以基本上你可以询问它所附加的视图并询问该视图。

    【讨论】:

      【解决方案3】:

      在 viewDidLoad 部分插入你的gestureRecognizer:

      - (void)viewDidLoad
      {
          (...)
      
          UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapTable:)];
          doubleTap.numberOfTapsRequired = 2;
          doubleTap.numberOfTouchesRequired = 1;
          [self.yourTable addGestureRecognizer:doubleTap];
      
          (...)
      
      }
      

      如果您只想检测单击,请将 doubleTap.numberOfTapsRequired 更改为 1。

      然后添加以下方法。这将检查点击点是否在节标题内:

      -(void)doubleTapTable:(UISwipeGestureRecognizer*)tap
      {
          if (UIGestureRecognizerStateEnded == tap.state)
          {
              CGPoint p = [tap locationInView:tap.view];
      
              NSIndexPath* indexPath = [yourTable indexPathForRowAtPoint:p];
      
              if(indexPath){  // user taped a cell
      
                  // whatever you want to do if user taped cell
      
              } else { // otherwise check if section header was clicked 
      
                  NSUInteger i;
      
                  for(i=0;i<[yourTable numberOfSections];i++) {
      
                      CGRect headerViewRect = [yourTable rectForHeaderInSection:i];
      
                      BOOL isInside = CGRectContainsPoint (headerViewRect,
                                                           p);
                      if(isInside) {
      
                          // handle Header View Selection
      
                          break;
                      }
      
                  }
              }
      
          }
      }
      

      【讨论】:

        【解决方案4】:

        在这里聚会有点晚了,但这可能是一个难以解决的问题,特别是如果(正如 @klyngbaek 在 cmets 中提到的那样),您正在添加/删除部分。通过重新加载整个部分来更改标题 UIView 上的标记或自定义索引属性可能会导致丑陋的动画。

        尝试将此作为手势识别器的回调方法,该方法附加到每个标头 UIView(诚然是 hackey):

        - (void)headerTapped:(UITapGestureRecognizer *)sender{
        
            NSInteger section = 0;
            for(int counter = 0; counter < [self.tableViewOfInterest numberOfSections]; counter++){
                if([[self.tableViewOfInterest headerViewForSection:counter] frame].origin.y == sender.view.frame.origin.y){
                    section = counter;
                    break;
                }
            }
        
        }
        

        基本上,当为每个节标题询问UITableView 时,它会返回一个标题实例,其中框架设置为标题在表中的位置。将其与 UITapGestureRecognizer 的 view 属性的框架进行比较,将在某个时候匹配(不是双关语)!

        【讨论】:

          【解决方案5】:

          您可以将按钮添加到标题并将标签设置为按钮,如下所示:

          - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
          (NSInteger)section {
              UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.height, tableView.frame.size.width)];
              UIButton *button = [[UIButton alloc] initWithFrame:headerView.frame];
              button.tag = section;
              [button addTarget:self action:@selector(detectSection:) forControlEvents:UIControlEventTouchUpInside];
              [headerView addSubView:button];
              return headerView;
          }
          
          -(void)detectSection:(UIButton *)sender {
              switch(sender.tag) {
              //your code
              }
          }
          

          【讨论】:

          • 虽然这段代码可以回答这个问题,但最好在不介绍其他代码的情况下解释它是如何解决问题的,以及为什么要使用它。从长远来看,纯代码的答案没有用处。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-02
          相关资源
          最近更新 更多