2014-05-24 2 views
1

UITableView의 각 섹션 헤더에서 조치를 수신하는 UIButton이 있습니다. 탭이 발생하면, 나는 그것이 내가 그의 코드 설정 때문에 셀 아닌 뭔가 치면 얻을 수 있어야합니다 : 나는 섹션 헤더 버튼을 누릅니다tableview에서 포인트의 섹션 헤더를 확인할 수 없습니다.

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

매번를, 함수는 나에게의 인덱스 경로를 반환 해당 섹션의 첫 번째 행

제 : 1 행 : 0 터치 : 192.0 범위 : 220.0-264.0

무슨 일이 있었는지 알아 내기 위해 나는 로그 예를 들어 나이를 반환

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); 

이 부분을 코딩

터치가 셀의 rect에서 벗어나지 만 인덱스 경로를 눌렀다는 것을 의미합니다. 이 문제가 발생한 첫 번째 섹션 인 호기심은 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

귀하의 질문이 충분히 명확하지 않다고 생각합니다. 귀하의 질문은 무엇인가? 그리고 무엇을 성취하려고합니까? – Ricky

+1

섹션 헤더 뷰에서 버튼을 탭하는 순간에 터치 위치를 가져오고 tableview 함수를 사용하여 섹션 인덱스를 가져 오려고합니다. 문제는 어떤 행도 건드리지 않고 있지만 indexPathForRowAtPoint :는 여전히 하나를 반환한다는 것입니다. –

+0

허용 된 대답은 http://stackoverflow.com/questions/16945661/how-to-identify-the-section-number-of-a-section-header-view, 나를 위해 잘 작동합니다. – rdelmar

답변

0

은 내가하는 일은 내 헤더가 section 인스턴스 변수 (그리고 전혀 코드)와 UITableViewHeaderFooterView 서브 클래스를 볼 때 사용하는 것입니다. 요청 된 각 헤더를 만들 때 section을 설정합니다. 따라서 나는 헤더가 두드린 section을 보았다.

+0

실제 다운로드 할 수있는 예제 프로젝트는 다음과 같습니다. https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch08p451dynamicTableContent/ch21p718sections - 섹션 헤더를 두 번 탭하여 응답합니다 (축소 또는 확장). 섹션). – matt

관련 문제