我有UIButton
在UITableView
的每个部分标题中收到操作。当点击发生时,我需要能够获得它是否击中了不是单元格的东西,所以我设置了他的代码:
UITouch *touch = [[event touchesForView:deleteButton] anyObject];
CGPoint location = [touch locationInView:self.tableView];
if (![self.tableView indexPathForRowAtPoint:location]) {
//stuff
}
每次点击节标题按钮时,该函数都会返回相应节中第一行的索引路径。为了弄清楚发生了什么,我编写了这部分
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
意思是即使触摸超出了单元格的矩形,它也会触及索引路径。好奇的是,第一部分也发生了这个问题,确实设法进入if结构。
重要的代码是节头功能:
- (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;
}
}
}
}
答案 0 :(得分:0)
我所做的是使用带有section
实例变量的UITableViewHeaderFooterView子类作为我的标题视图(根本没有代码)。当我创建每个请求的标头时,我设置了section
。因此,我知道只需查看section
哪个标题被点击。