2013-03-26 57 views
6
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return UITableViewCellEditingStyleDelete; 
} 


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

    if(editingStyle == UITableViewCellEditingStyleDelete){ 
     //[theSimpsons removeObjectAtIndex:indexPath.row]; 

     NSString *time = [[arrayList objectAtIndex:indexPath.row] objectForKey:@"TIME"]; 
     NSString *date= [[arrayList objectAtIndex:indexPath.row] objectForKey:@"DATE"]; 
     DBManager *dbm = [[DBManager alloc] init]; 
     [dbm initiallzeDatabase]; 
     [dbm deleteItemAtIndexPath:time :date]; 

     //arrayList = [dbm getParkResults]; 
     //[parkTableView reloadData]; 

     [arrayList removeObjectAtIndex:indexPath.row]; 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

Tôi đã sử dụng mã ở trên trong ứng dụng của mình. Phần chỉnh sửa hoạt động tốt, nhưng vấn đề là, nút xóa chỉ xuất hiện khi chúng tôi vuốt từ trái sang phải trên một ô. Bây giờ tôi có một menu mở ra từ trái sang phải như Facebook. Vậy làm cách nào tôi có thể đảm bảo rằng nút xóa xuất hiện khi người dùng vuốt từ phải sang trái.Muốn hiển thị nút xóa trên vuốt từ phải sang trái trong UITableView

Trả lời

2

này có thể làm việc. Đặt mã này trong awakeFromNib hoặc init:

UISwipeGestureRecognizer *swipeDelete = [[UISwipeGestureRecognizer alloc]  initWithTarget:self action:@selector(handleSwipeDelete:)]; 
[swipeDelete setDirection: UISwipeGestureRecognizerDirectionLeft]; 
[yourTableView addGestureRecognizer:swipeDelete]; 

sau đó trong handleSwipeDelete tổ chức di động của bạn và hiển thị nút delete. Bạn có thể sẽ cần phải xử lý trạng thái di động và thay đổi nó trên swipeLeft/swipeRight.

+0

Tôi đưa ra một thử này. Khi bạn nói hiển thị nút Xóa, chúng tôi có thể hiển thị nút xóa mặc định theo chương trình không? Nếu có thì làm thế nào? Cảm ơn :) – ScarletWitch

+1

Bạn có thể thử đặt loại ô bảng thành 'UITableViewCellEditingStyleDelete' khi vuốt sang trái. Hoặc tạo lớp con của 'UITableViewCell' và vẽ ô đó thay vì ô mặc định cho hàng đó. – Gossamer

+0

Vâng. Điều này được thực hiện công việc vừa phải. Cảm ơn Gossamer. :) – ScarletWitch

0

vẻ như bạn cần để tùy chỉnh hành vi của UITableViewCell, tôi chưa bao giờ nhận thấy rằng Apple đã cung cấp hành vi sau cách vuốt sang phải sang trái ....

6

Xin vui lòng không thiết lập các đại biểu để UISwipeGestureRecognizer

Viết này trong viewDidLoad hoặc nơi bạn đã khởi xướng của bạn UITableView

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(callYourMethod:)]; 
swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
[self.tblView addGestureRecognizer:swipeRight]; 

Không cần phải viết bất cứ điều gì trong phương pháp dưới đây.

- (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer 
{ 

} 
+0

Cảm ơn Manohar. Tôi đã nhận được câu trả lời bằng cách kết hợp ý tưởng của bạn và @ Gossamer. Cảm ơn cả hai bạn. Bạn đã cứu tôi rất nhiều công việc. :) – ScarletWitch

1

Bạn có thể nhận nó bằng cách sử dụng two Methods of UITableVie.

// This only needs to be implemented if you are going to be returning NO 
// for some items. By default, all items are editable. 
- (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 
    //When you remove a cell of your tableview, you also has to remove your array object at index x 
    }  
} 

Sau khi loại bỏ các đối tượng. Bạn phải tải lại UITableView.

[self.tableView reloadData]; 

Để biết thêm thông tin Read This OfficialDocumentaion.

+0

Có cảm ơn @iPatel. Nhưng như tôi đã nói. các cử chỉ không thể được đọc coz am trượt trong menu tại cùng một sự kiện. Vì vậy, điều này không hoạt động. Nhưng cảm ơn anyways. :) – ScarletWitch

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