2014-07-24 21 views
5

Tôi muốn xử lý một lần nhấn trong một số UITableView không bao gồm các ô. Nói cách khác, đầu trang và chân trang.Làm cách nào để phát hiện các vòi trên UITableView bên ngoài UITableViewCells - tức là trong tiêu đề phần?

Tôi có thể thêm trình nhận dạng cử chỉ vào tiêu đề (không có chân trang), nhưng khoảng trống ở cuối phần cuối không phản hồi với lần nhấn.

Thay vì thêm công cụ nhận dạng cử chỉ ở trên, tôi đã thử thêm trình nhận dạng cử chỉ vào bảng nhưng ngăn chặn được gọi là tableView:didSelectRowAtIndexPath:.

Tôi đã thử tất cả các loại cuộc gọi UIGestureRecognizerDelegate không có nhiều may mắn.

cuối cùng tôi nhận được rằng để làm việc bằng cách thiết lập cancelsTouchesInView = NO trên recognizer (Tôi nghĩ rằng đây là bí mật) và thực hiện gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: trong các đại biểu để các tableView đã chạm và gọi tableView:didSelectRowAtIndexPath:

Việc xử lý có để lọc ra các vòi trên các tế bào, nhưng ít nhất nó hoạt động. (Ngoài ra, tìm thấy một lỗi trong UITableViewindexPathForRowAtPoint:point mà trả về không hợp lệ indexPaths đôi khi.)

Câu hỏi của tôi là: Có cách nào tốt hơn để có được quan điểm di động để ngăn chạm/cử chỉ vào bàn? (Tôi sẽ đăng mã nếu không ai cho tôi bất kỳ ý tưởng nào tốt hơn.)

+0

Xem câu trả lời của tôi làm việc của mình ... –

Trả lời

3

Dưới đây là đoạn code mà tôi đã đi với.

1 Thêm trình nhận dạng cử chỉ nhấn vào bảng. Đặt Hủy chạm ở chế độ xem không được chọn.

2 Cho phép cử chỉ thông qua bảng.

#pragma mark - UIGestureRecognizerDelegate 

/** 
* Prevents the tap gesture recognizer in the table from gobbling taps 
* and allows the table to perform tableView:didSelectRowAtIndexPath: 
* <p> 
* The gesture recognizer must have cancelsTouchesInView == NO to allow 
* the touches through to the table. 
* <p> 
* The action must check that the tap occurred outside of cells. 
* 
* @see handleTap: 
*/ 
- (BOOL) gestureRecognizer:(UIGestureRecognizer*)recognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)o { 
    return recognizer.view == self.table; 
} 

3 Kiểm tra vòi không nằm trong ô.

/** 
* Filters a tap on the table to those on the headers/footer. 
* <p> 
* Note, indexPathForRowAtPoint: has a bug and returns valid indexPaths 
* for taps in the last section header and table footer. So an extra 
* check is made to ensure that the tap was, in fact, in the cell. 
*/ 
- (IBAction) handleTap:(UITapGestureRecognizer*)tap { 
    if (UIGestureRecognizerStateEnded == tap.state) { 
     CGPoint point = [tap locationInView:tap.view]; 
     NSIndexPath* index = [self.table indexPathForRowAtPoint:point]; 
     UITableViewCell* cell; 

     if (index) { 
      cell = [self.table cellForRowAtIndexPath:index]; 
      point = [tap locationInView:cell]; 

      if (point.y < 0 || point.y >= cell.frame.size.height) { 
       index = nil; 
      } 
     } 

     if (!index) { 
      [self.view performSelector:@selector(endEditing:) 
          withObject:@(YES) afterDelay:0]; 
     } 
    } 
} 
4

Nếu bạn không cần phải hiểu - những gì được nhấp (chân trang hoặc tiêu đề), bạn có thể thử cách tiếp cận sau.

Thêm trình nhận dạng cử chỉ trong phương thức viewDidLoad.

- (void) tableViewLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { 
    CGPoint p = [gestureRecognizer locationInView:self.tableView]; 

    NSIndexPath *indexPath = [self.messageTableView indexPathForRowAtPoint:p]; 
    if (indexPath == nil) 
     NSLog(@"long press on table view but not on a row"); 
    else { 
     NSLog(@"long press on table view on a row"); 
    } 
} 

Chú ý! Tôi chưa thử nghiệm mã này :)

1

Thêm công cụ nhận dạng cử chỉ vào chế độ xem đầu trang và chân trang của phần của bạn là chính xác.

Để repsond chạm vào khu vực bên dưới hàng cuối cùng, hãy thêm chế độ xem chân trang vào bảng. Thêm trình nhận dạng cử chỉ vào chân trang bảng này. Làm cho chế độ xem chân trang này cao bằng màn hình.

Nhược điểm duy nhất cho điều này là bạn sẽ mất các dòng bên dưới hàng cuối cùng nếu các hàng không lấp đầy màn hình. Nếu bạn thực sự muốn chúng, hãy thiết lập chế độ xem chân trang bảng tùy chỉnh của bạn để vẽ các đường để bắt chước các hàng.

2

Bạn đã thử bằng cách thêm longGesture trong viewForHeaderInSection ??

Nếu không hơn làm theo điều này:

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

    UILabel *headerLabel = [[UILabel alloc]init]; 
    headerLabel.tag = section; 
    headerLabel.userInteractionEnabled = YES; 
    headerLabel.backgroundColor = [UIColor greenColor]; 
    headerLabel.text = [NSString stringWithFormat:@"Header.%d",section]; 
    headerLabel.frame = CGRectMake(0, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height); 


    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hederClicked:)]; 
    tapGesture.cancelsTouchesInView = NO; 
    [headerLabel addGestureRecognizer:tapGesture]; 

    return headerLabel; 

    //return nil; 
} 

-(void)hederClicked:(UIGestureRecognizer*)sender 
{ 
    UILabel *lbl = (UILabel*)sender.view; 
    NSLog(@"header no : %d", lbl.tag); 
} 
1

Thêm Header Xem trong tableView của bạn như sau mã:

Chiều cao để xem tiêu đề trong bảng

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 100; 
} 

Ngoài ra thêm xem trong header và tapgesture trong khung nhìn này.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)]; 
    /* Create custom view to display section header... */ 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 100)]; 
    [label setFont:[UIFont boldSystemFontOfSize:12]]; 

    /* Section header is in 0th index... */ 
    [label setText:@"Header View"]; 
    [viewHeader addSubview:label]; 
    [viewHeader setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; 


    // Add TapGestureRecognizer 

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; 

    [recognizer setNumberOfTapsRequired:1]; 
    recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view 
    [viewHeader addGestureRecognizer:recognizer]; 

    return viewHeader; 
} 

TapGestureRecognizer mehode

- (void)handleTapBehind:(UITapGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateEnded) 
    { 
     [[[UIAlertView alloc]initWithTitle:@"Demo" message:@"Test On header Click" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; 
    } 
} 

Output của bạn:

enter image description here

1

Đây là việc thực hiện hiệu quả nhất đơn giản nhất:

Sử dụng các UITableViewDelegate phương pháp (iOS6 +):

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger) 

- (void)tableView:(UITableView *)tableView 
    willDisplayFooterView:(UIView *)view 
    forSection:(NSInteger)section 

Bạn có thể sử dụng các phương pháp này để dễ dàng thêm một UITapGestureRecognizer đến một phần footer/tiêu đề.

Ex:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
    { 
     UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)]; 

     [view addGestureRecognizer:recognizer]; 

    } 

    - (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer { 
    NSLog(@"Tapped"); 
    } 
Các vấn đề liên quan