2012-01-30 37 views

Trả lời

6

Tôi chưa thử và triển khai, nhưng tôi sẽ quay. Đầu tiên, tạo một UITableViewCell tùy chỉnh, và để cho nó có 2 tính năng mà bạn có thể sử dụng

  1. Một tham chiếu tới tableView nó đang được sử dụng trong.
  2. Một indexPath để tìm các tế bào trong tableView. (Lưu ý, điều này cần phải được cập nhật mỗi khi bạn xóa một ô cho tất cả các ô nơi thay đổi này)

Trong cellForRowAtIndexPath:, nơi bạn tạo ô tùy chỉnh, hãy đặt các thuộc tính này. Ngoài ra, hãy thêm UISwipeGestureRecognizer vào ô

cell.tableView=tableView; 
cell.indexPath=indexPath; 
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)]; 
[cell addGestureRecognizer:swipeGestureRecognizer]; 
[swipeGestureRecognizer release]; 

Đảm bảo rằng cử chỉ chỉ nhận được vuốt ngang.

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
{ 
    if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&& 
     ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft 
     ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES; 
} 

Trong bạn deleteCell:

-(void) deleteCell:(UIGestureRecognizer*)gestureRec 
{ 
    UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec; 
    CustomCell *cell=[swipeGestureRecognizer view]; 
    UITableView *tableView=cell.tableView; 
    NSIndexPath *indexPath=cell.indexPath; 
    //you can now use these two to perform delete operation 
} 
+0

Đừng quên rằng bạn cần phải thiết lập hướng cho bộ nhận dạng cử chỉ khi lần đầu tiên khởi tạo nó để thêm nó vào ô. Một cái gì đó như thế này: swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – ColossalChris

+0

Apple, đừng cho bạn không cung cấp '(void) cancelPreviousPerformRequestsWithTarget: (id) aTarget select: (SEL) aSelector'. –

-2

cho rằng u phải thêm mã này vào dự án của bạn.

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 
    }  
} 

thông tin khác mor u có thể đi qua liên kết này enter link description here

+0

Vivek2012 và iWill - Cảm ơn bạn đã trả lời nhưng tôi không muốn hiển thị nút DELETE, đã có câu trả lời cho những người trong stackoverflow. Tôi muốn ô bị xóa khỏi phút nhận được cử chỉ sang trái hoặc phải.xin vui lòng đọc mô tả của tôi nhờ – carbonr

-1

thực hiện

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; 

hai phương pháp này, nếu UITableViewCellEditingStyle là UITableViewCellEditingStyleDelete, sau đó làm sth để xóa di động của bạn với

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 

đó là tất cả.

3

Giải pháp đăng bởi @MadhavanRP hoạt động, nhưng nó là phức tạp hơn nó cần phải được. Bạn có thể thực hiện một cách tiếp cận đơn giản hơn và tạo một trình nhận dạng cử chỉ xử lý tất cả các lần vuốt xuất hiện trong bảng và sau đó lấy vị trí của thao tác vuốt để xác định ô nào người dùng đã vuốt.

Để thiết lập bộ nhận dạng cử chỉ:

- (void)setUpLeftSwipe { 
    UISwipeGestureRecognizer *recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                 action:@selector(swipeLeft:)]; 
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.tableView addGestureRecognizer:recognizer]; 
    recognizer.delegate = self; 
} 

Gọi phương pháp này trong viewDidLoad

Để xử lý các swipe:

- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { 
    CGPoint location = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
    ... do something with cell now that i have the indexpath, maybe save the world? ... 
} 

lưu ý: vc bạn phải thực hiện các đại biểu cử chỉ recognizer .

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