2015-06-06 44 views
2

我想讓用戶能夠刪除表格單元格的時刻我有刪除刷卡動作,但我不知道如何實際刪除選定的單元格!正如你可以告訴我在雨燕的Xcode 6.3.1UITableViewRowAction:刪除

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

編程這裏是Dropbox的截圖,以及:Screenshot Of Delete Swipe Action

回答

1

如果你想從的tableView完全刪除單元格,那麼你已經從你的表陣列也將其刪除。考慮下面的代碼:

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] 
} 
+0

這一個工作,我有我的數組一些錯誤,但那是因爲它是一個讓不是變種!雖然謝謝! –

1

這可以幫助你。

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) 

    } 
} 
+0

當我將其添加到提交編輯樣式和添加項目我自己的數組(這是「產品」),我得到的「類型的永恆價值‘[字符串]’錯誤不僅具有變異名爲'removeAtIndex'的會員@RandomBytes –

+0

嘗試Dharmesh Kheni的建議,這可能是解決方案。我太醉了,真的可以幫到更多的時間。祝你好運。 – RandomBytes

1

您必須實現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