2010-12-15 38 views
13

Tôi có UIView bao gồm tất cả UITableView. UIView đang sử dụng trình nhận dạng cử chỉ để kiểm soát những gì bảng hiển thị. Tôi vẫn cần các cuộn hàng và cuộn UITableView dọc. Làm cách nào để chuyển các bảng này vào bảng từ trình nhận dạng cử chỉ?Trình nhận dạng cử chỉ và Bảng nhìn

+0

Tôi tin "[tablView cử chỉ]" nên được "[yourTableView addGestureRecognizer: cử chỉ]" –

Trả lời

30

Gán cử chỉ của bạn để xem bảng và bảng sẽ chăm sóc nó:

UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] 
     initWithTarget:self action:@selector(handleSwipeFrom:)]; 
[gesture setDirection: 
     (UISwipeGestureRecognizerDirectionLeft 
     |UISwipeGestureRecognizerDirectionRight)]; 
[tableView addGestureRecognizer:gesture]; 
[gesture release]; 

Sau đó, trong phương pháp hành động cử chỉ của bạn, hành động dựa trên sự chỉ đạo:

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { 
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { 
     [self moveLeftColumnButtonPressed:nil]; 
    } 
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) { 
     [self moveRightColumnButtonPressed:nil]; 
    } 
} 

bảng sẽ chỉ truyền cho bạn những cử chỉ bạn đã yêu cầu sau khi xử lý chúng trong nội bộ.

+0

này là câu trả lời tuyệt vời. Nhưng bạn có thể vui lòng cho tôi biết làm thế nào để biết tableview indexPath trong phương thức handleSwipeFrom? – dinesh

+4

Điều này sẽ không hoạt động - thuộc tính định hướng trên UISwipeGestureRecognizer cho biết hướng nào CÓ THỂ được nhận dạng (trong trường hợp này là trái VÀ phải) chứ không phải hướng WAS bị quét. Bạn cần phải tách riêng các trình nhận dạng cử chỉ –

+1

Có, recognizer.direction sẽ luôn là 3 và không bao giờ khớp. Điều này được cho là một lỗi: http://stackoverflow.com/questions/3319209/setting-direction-for-uiswipegesturerecognizer – Symmetric

31

Nếu bạn cần biết indexPath của tế bào của bạn:

- (void)handleSwipeFrom:(UIGestureRecognizer *)recognizer { 
    CGPoint swipeLocation = [recognizer locationInView:self.tableView]; 
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation]; 
    UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath]; 
} 

này trước đây đã trả lời trong UIGestureRecognizer and UITableViewCell issue.

+0

Cảm ơn bạn, tôi đã tìm kiếm giải pháp này. Tôi không có đầu mối 'locationInView' cảm ơn rất nhiều – carbonr

+0

Lưu thịt xông khói của tôi, cảm ơn bạn đời! – jcrowson

7

Tôi đã thử đề xuất của Rob Bonner và nó hoạt động rất tốt. cảm ơn bạn.

Nhưng, trong trường hợp của tôi, có vấn đề với nhận dạng hướng. (recognizer.direction luôn luôn tham khảo 3) Tôi đang sử dụng SDK IOS5 và Xcode 4.

Có vẻ như do "[seture setDirection: (left | right)]" Tôi nghĩ vậy. (vì kết quả tính toán được xác định trước (dir left | dir right) là 3)

Vì vậy, nếu ai đó gặp vấn đề như tôi và muốn nhận dạng vuốt sang trái và phải một cách riêng biệt, hãy gán hai công cụ nhận dạng bảng với các hướng khác nhau.

Như thế này:

UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(handleSwipeLeft:)]; 
[swipeLeftGesture setDirection: UISwipeGestureRecognizerDirectionLeft]; 

UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] 
               initWithTarget:self 
               action:@selector(handleSwipeRight:)]; 

[swipeRightGesture setDirection: UISwipeGestureRecognizerDirectionRight]; 

[tableView addGestureRecognizer:swipeLeftGesture]; 
[tableView addGestureRecognizer:swipeRightGesture]; 

và cử chỉ hành động dưới đây:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer { 
    [self moveLeftColumnButtonPressed:nil]; 
} 

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer { 
    [self moveRightColumnButtonPressed:nil]; 
} 

tôi mã hóa với tính năng ARC sau đó nếu bạn không sử dụng ARC, thêm mã phát hành.

PS: tiếng Anh của tôi không phải là quá tốt, vì vậy nếu có bất kỳ lỗi sentential, sửa chữa sẽ rất vui :)

Các vấn đề liên quan