Determine section in UITableView from CGPoint

2019-09-01 09:36发布

问题:

I need to determine section in UITableView based on CGPoint. With section I mean section header + cells + section footer. There are only methods rectForHeaderInSection: and rectForFooterInSection: but nothing for whole section. Thanks.

回答1:

You would have to manually iterate all possible sections and determine if a point exists for a given section.

There is a rectForSection: method on UITableView's that should combine the rectangles of the section header, cells, and footer. You could emulate that by doing a CGRectUnion of the rectForHeaderInSection: and rectForFooterInSection: for a given section (assuming those both existed).

NSInteger tappedSection = -1;
for (NSInteger section = 0; section < tableView.numberOfSections && tappedSection < 0; section++) {
    CGRect sectionRect = [tableView rectForSection:section];
    if (CGRectContainsPoint(sectionRect, somePoint)) {
        tappedSection = section;
    }
}
if (tappedSection >= 0) {
    NSLog(@"Tapped: %d", tappedSection);
}