无法从 tableView 中的点确定节标题 [英] Can't determine section header from point in tableView

查看:18
本文介绍了无法从 tableView 中的点确定节标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIButtonUITableView 的每个部分标题中接收一个动作.当点击发生时,如果它击中不是单元格的东西,我需要能够得到,所以我设置了他的代码:

I have a UIButton receiving an action in each section header in a UITableView. When a tap occurs, I need to be able to get if it hit something that is not a cell so I set his code:

UITouch *touch = [[event touchesForView:deleteButton] anyObject];
CGPoint location = [touch locationInView:self.tableView];
if (![self.tableView indexPathForRowAtPoint:location]) {
  //stuff
}

每次我点击部分标题按钮时,该函数都会返回相应部分第一行的索引路径.为了弄清楚发生了什么,我对这部分进行了编码

Everytime I tap a section header button, the function returns me the index path of the first row from the corresponding section. In order to figure out what was going on I coded this part

UITouch *touch = [[event touchesForView:deleteButton] anyObject];
CGPoint location = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
CGRect frame = [self.tableView rectForRowAtIndexPath:indexPath];
NSLog(@"\nSection: %ld\nRow:%ld\nTouch: %.f\nRange: %.1f-%.1f\n\n", indexPath.section, indexPath.row, location.y, frame.origin.y, frame.origin.y + frame.size.height);

日志返回给我,例如:

部分:1行:0触摸:192.0范围:220.0-264.0

Section: 1 Row: 0 Touch: 192.0 Range: 220.0-264.0

这意味着即使触摸超出了单元格的矩形,它也会击中索引路径.奇怪的是,第一部分也发生了这个问题,确实设法进入了 if 结构.

Meaning that even though the touch was out of the cell's rect it hit an index path. Curious enough, the first section in which also happens this problem, does manage to enter the if-structure.

重要的代码是节头函数:

The important code would be the section header function:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UIView *headerView = [UIView new];
  UILabel *headerLabel = [UILabel new];
  headerLabel.frame = CGRectMake(ZTRowPadding, 2.0, CGRectGetWidth(tableView.frame) - 2 * ZTRowPadding - ZTSectionHeaderHeight, 40.0);
  headerLabel.text = [self.wod.wSets[section] fullSizeSectionTitle:NO];
  headerLabel.textColor = [UIColor carbonColorWithAlpha:ZTAlphaLevelFull];
  headerLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:13.0];
  headerLabel.numberOfLines = 2;
  [headerView addSubview:headerLabel];

  UIButton *deleteButton = [UIButton new];
  deleteButton.frame = CGRectMake(CGRectGetWidth(tableView.frame) - ZTSectionHeaderHeight, 0.0, ZTSectionHeaderHeight, ZTSectionHeaderHeight);
  deleteButton.backgroundColor = [UIColor crimsonColorWithAlpha:ZTAlphaLevelFull];
  [deleteButton setTitle:@"X" forState:UIControlStateNormal];
  [deleteButton setTitleColor:[UIColor ivoryColorWithAlpha:ZTAlphaLevelFull] forState:UIControlStateNormal];
  [deleteButton addTarget:self action:@selector(deleteSectionButtonTapped:forEvent:) forControlEvents:UIControlEventTouchUpInside];
  [headerView addSubview:deleteButton];

  return headerView;
}

和删除按钮操作.

- (void)deleteSectionButtonTapped:(UIButton *)deleteButton forEvent:(UIEvent *)event {
  UITouch *touch = [[event touchesForView:deleteButton] anyObject];
  CGPoint location = [touch locationInView:self.tableView];
  NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  CGRect frame = [self.tableView rectForRowAtIndexPath:indexPath];
  NSLog(@"\nSection: %ld\nRow:%ld\nTouch: %.f\nRange: %.1f-%.1f\n\n", indexPath.section, indexPath.row, location.y, frame.origin.y, frame.origin.y + frame.size.height);

  if (![self.tableView indexPathForRowAtPoint:location]) {
    for (NSInteger section = 0; section < [self.wod.wSets count]; section++) {
      CGRect headerViewRect = [self.tableView rectForHeaderInSection:section];
      if(CGRectContainsPoint (headerViewRect, location)) {
        ZTWodSet *wodSet = self.wod.wSets[section];
        [self.wod removeObject:wodSet fromOrderedSetWithKey:@"wSets"];

        [[ZTDataManager sharedDataManager].mainContext deleteObject:wodSet];
        NSError *error;
        if (![[ZTDataManager sharedDataManager].mainContext save:&error]) {
          NSLog(@"Failed to save data");
          return;
        }

        [self.tableView beginUpdates];
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
        break;
      }
    }
  }
}

推荐答案

我所做的是将 UITableViewHeaderFooterView 子类与 section 实例变量(完全没有代码)用作我的标题视图.当我创建每个请求的标头时,我设置了 section.因此,我只需查看 section 哪个标题被点击就知道了.

What I do is to use as my header view a UITableViewHeaderFooterView subclass with a section instance variable (and no code at all). When I create each requested header I set the section. Thus I know just by looking at the section which header was tapped.

这篇关于无法从 tableView 中的点确定节标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆