问题
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);
The log returns me this for example:
Section: 1 Row: 0 Touch: 192.0 Range: 220.0-264.0
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;
}
and the delete button action.
- (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;
}
}
}
}
回答1:
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.
来源:https://stackoverflow.com/questions/23840496/cant-determine-section-header-from-point-in-tableview