簡體   English   中英

UITableViewRowAction:刪除

[英]UITableViewRowAction: Delete

我想讓用戶能夠在執行刪除滑動操作時刪除表格單元格,但是我不知道如何實際刪除所選單元格! 如您所知,我正在使用Swift和Xcode 6.3.1進行編程

var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in
            tableView.editing = false
            println("deleteAction")
            }

這也是Dropbox上的屏幕截圖: Delete Swipe Action的屏幕截圖

如果要從tableView完全刪除單元格,則也必須從表Array中刪除它。 考慮下面的代碼:

var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in
        tableView.editing = true
            // Remove it from your TableArray and If it is stored into any local storage then you have to remove it from there too because if you doesn't remove it from your local storage then when you reload your tableview it will appears back

            self.tableData.removeAtIndex(indexPath.row) 

            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

    }

    return [deleteAction]
}

這可能對您有幫助。

  func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        // handle delete (by removing the data from your array and updating the tableview)
        if let tv=tableView
        {
         items.removeAtIndex(indexPath!.row)
            tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

    }
}

您必須實現editActionsForRowAtIndexPathcommitEditingStyle

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

    __weak SampleViewController *weakSelf = self;

    UITableViewRowAction *actionRed =
    [UITableViewRowAction
     rowActionWithStyle:UITableViewRowActionStyleNormal
     title:@"Delete"
     handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
         NSLog(@"Delete!");

         [weakSelf.itemsList removeObjectAtIndex:indexPath.row];
         [weakSelf.tableView setEditing:NO animated:YES];
         [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath]
                                   withRowAnimation:UITableViewRowAnimationAutomatic];

     }];

    actionRed.backgroundColor = [UIColor colorWithRed:0.844 green:0.242 blue:0.292 alpha:1.000];


    return @[actionRed];
}


/*
 * Must implement this method to make 'UITableViewRowAction' work.
 *
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

}

這是一個示例ActionRowTest

暫無
暫無

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

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