簡體   English   中英

如何啟用滑動以刪除TableView中的單元格?

[英]How to enable swipe to delete cell in a TableView?

我有一個UIViewController實現了TableViews委托和數據源協議。 現在我想向細胞添加“輕掃刪除”手勢。

我應該怎么做呢。

我給出了一個commitEditingStyle方法的空白實現,並將Editing屬性設置為YES。

仍然沒有滑動功能。

現在我需要單獨向每個單元格添加UISwipeGesture嗎?

或者我錯過了什么?

正如Dan上面所述,您需要實現以下表視圖委托方法:

  1. tableView:canEditRowAtIndexPath:
  2. tableView:commitEditingStyle:forRowAtIndexPath:

注意:我在iOS 6和iOS 7中嘗試過這個。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return YES - we will be able to delete all rows
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Perform the real delete action here. Note: you may need to check editing style
    //   if you do not perform delete only.
    NSLog(@"Deleted row.");
}

您不必設置editing:YES如果您需要在單元格滑動上顯示刪除按鈕,則為editing:YES 您必須實現tableView:canEditRowAtIndexPath:並從那里為您需要編輯/刪除的行返回YES。 當tableView的dataSource是UITableViewContoller的子類時,這不是必需的 - 如果不覆蓋此方法,則默認情況下返回YES。 在所有其他情況下,您必須實現它。

編輯:我們一起發現了問題 - tableView:editingStyleForRowAtIndexPath:如果表未處於編輯模式,則返回UITableViewCellEditingStyleNone

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not 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) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

請在swift中嘗試此代碼,

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
   // let the controller to know that able to edit tableView's row 
   return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)  {
   // if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code)
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?  {
   // add the action button you want to show when swiping on tableView's cell , in this case add the delete button.
   let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in

   // Your delete code here.....
   .........
   .........
   })

   // You can set its properties like normal button
   deleteAction.backgroundColor = UIColor.redColor()

   return [deleteAction]
}

嘗試將以下內容添加到您的班級:

// Override to support conditional editing of the table view.
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return(YES);
}

結論Kyr Dunenkoff聊天是

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

}

如果您需要在滑動時顯示刪除按鈕,則不應定義。

如果您使用NSFetchedResultsControllerDelegate填充表視圖,這對我NSFetchedResultsControllerDelegate

  • 確保tableView:canEditRowAtIndexPath始終返回true
  • tableView:commitEditingStyle:forRowAtIndexPath實現中,不要直接從表視圖中刪除該行。 而是使用您的托管對象上下文刪除它,例如:

     if editingStyle == UITableViewCellEditingStyle.Delete { let word = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Word self.managedObjectContext.deleteObject(word) self.saveManagedObjectContext() } func saveManagedObjectContext() { do { try self.managedObjectContext.save() } catch { let saveError = error as NSError print("\\(saveError), \\(saveError.userInfo)") } } 

對我來說這也是一個問題......我只能每10次左右嘗試一次刷到刪除工作。 原來, gesture正在阻止在父視圖控制器另一手勢在電視機上。 電視嵌套在MMDrawerController (可滑動的抽屜布局)中。

只是在抽屜控制器中配置手勢識別器以不響應側翼抽屜中的近距離手勢,允許滑動刪除以在我的電視中工作。

您也可以嘗試使用gesture delegate做類似的事情:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

這是快速版本

// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

根據我的經驗,您似乎必須將UITableView editing設置為NO才能滑動才能工作。

self.tableView.editing = NO;

在iOS 8.0之后,您可以自定義您的操作

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil]; 


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSUInteger count = [posts count];

    if (row < count) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}

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

    NSUInteger row = [indexPath row];
    NSUInteger count = [posts count];

    if (row < count) {

        [posts removeObjectAtIndex:row];
    }
}

您可以通過在XCode 5中創建UITableViewController類(臨時)來查看所有必需的方法,然后復制您要使用的方法。 您需要的那些方法將用您想要的預先填充的行注釋掉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM