2015-03-27 18 views
11

Tôi đang cố xóa một hàng trong chế độ xem bảng. Tôi đã thực hiện các phương pháp cần thiết nhưng khi tôi đang vuốt hàng theo chiều ngang không có nút xóa nào đang đến. Tôi đã tìm kiếm và tôi đã tìm thấy cùng một giải pháp ở mọi nơi nhưng nó không hoạt động trong trường hợp của tôi. Tôi không biết tôi đang phạm sai lầm ở đâu. Có ai có thể giúp tôi không?xóa một hàng trong chế độ xem bảng trong swift

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
    return true 
} 

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == .Delete 
    { 
     dataHandler.deletePeripheral(indexPath.row) 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } 
} 
+0

tôi nghĩ rằng u kiểm tra cú pháp hàm –

+0

func tableView (tableView: UITableView, commitEditingStyle ed itingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { –

+0

@LeonardoSavioDabus Tôi đã sử dụng cú pháp tương tự như bạn đã đề xuất và tôi cũng đã cập nhật câu hỏi của mình nhưng vẫn chưa xóa nút. –

Trả lời

38

Nếu bên dưới mã hóa là hữu ích cho bạn, tôi sẽ được hạnh phúc

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
    return true 
} 

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == .Delete 
    { 
     yourArray.removeAtIndex(indexPath.row) 
     self.tableView.reloadData() 
    } 
} 

SWIFT 3,0

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool 
{ 
    return true 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) 
{ 
    if editingStyle == .delete 
    { 
     yourArray.remove(at: indexPath.row) 
     tblDltRow.reloadData() 
    } 
} 

Bạn phải làm mới bảng.

+11

tableView.deleteRowsAtIndexPaths ([indexPath], withRowAnimation: .Fade) là một ý tưởng tốt hơn là tải lại toàn bộ bảng –

2
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
if editingStyle == UITableViewCellEditingStyle.Delete { 
    numbers.removeAtIndex(indexPath.row)  
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
} 
} 
+0

i am sử dụng bảng xem không bộ điều khiển xem bảng tôi không thể sử dụng ghi đè và tôi đã thực hiện phương pháp theo cùng một cách nhưng nó không hoạt động. –

7
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

     if (editingStyle == UITableViewCellEditingStyle.Delete) { 


      self.tableView.beginUpdates() 
      self.arrayData.removeObjectAtIndex(indexPath.row) // also remove an array object if exists. 
      self.tableView.deleteRowsAtIndexPaths(NSArray(object: NSIndexPath(forRow: indexPath.row, inSection: 2)), withRowAnimation: UITableViewRowAnimation.Left) 
      self.tableView.endUpdates() 

     } 
+2

BeginUpdate() và endUpdate() đã giải quyết được sự cố của tôi khi ứng dụng gặp sự cố khi tôi chỉ cố gắng gọi DeleteRow trong Swift 3 –

0

tôi biết những gì prob bạn đang gặp. Tôi nghĩ rằng bạn chỉ cần kiểm tra các ràng buộc bảng của bạn họ có thể sai chỉ kiểm tra lại tự hạn chế bố trí của bạn

3

Áp dụng Hạn chế tự động điền cho chế độ xem bảng. Nút delete là có nhưng không hiển thị vì không có bố trí tự động

+0

Tôi đã có (một số) ràng buộc nhưng dường như không đủ. Autolayout là một nghệ thuật, không phải là một khoa học.:) – mobibob

0
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
       if editingStyle == UITableViewCellEditingStyle.Delete { 
    yourArray.removeAtIndex(indexPath.row) 

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

    } 

} 
0
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
return true 
} 

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
if editingStyle == .Delete 
{ 
    carsArray.removeAtIndex(indexPath.row) 
    self.tableView.reloadData() 
} 
} 
1

tôi vật lộn với điều này cũng nhưng cuối cùng phát hiện ra một giải pháp - Tôi đang chạy XCode 8.3.2. Rất nhiều câu trả lời tôi đọc là dành cho các phiên bản cũ của XCode/Swift và nhiều phiên bản dành cho Objective-C.

Giải pháp tôi tìm thấy là sử dụng công cụ 'editActionsForRowAt' cho UITableViews. Lưu ý: Mọi thứ khác tôi đọc tiếp tục chỉ cho tôi về công cụ 'commit editingStyle' cho UITableView, nhưng tôi không bao giờ có thể làm việc đó. Sử dụng editActionsForRowAt hoạt động hoàn hảo cho tôi.

LƯU Ý: 'postData' là tên của Mảng mà tôi đã thêm dữ liệu vào.

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in 
     // Remove item from the array 
     postData.remove(at: IndexPath.row) 

     // Delete the row from the table view 
     tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade) 
     }) 
     // set DeleteAction Button to RED (this line is optional - it simply allows you to change the color of the Delete button - remove it if you wish) 
     deleteAction.backgroundColor = UIColor.red 

    return [deleteAction] 
} 

Tôi cũng có một số mã để thêm một 'nút EDIT' bên cạnh nút Delete:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, IndexPath) in 
     //print("Edit tapped") 
     }) 
     // Set EditAction button to blue (this line is not mandatory - it just sets the color of the Edit box to blue) 
     editAction.backgroundColor = UIColor.blue 

    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in 
     //print("Delete tapped") 
     // Remove item from the array 
     postData.remove(at: IndexPath.row) 

     // Delete the row from the table view 
     tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade) 
     }) 
     // set DeleteAction Button to RED (this line isn't mandatory - it just sets the color of the Delete box) 
     deleteAction.backgroundColor = UIColor.green 

    return [editAction, deleteAction] 
} 

EDIT: cố định định dạng do đó, mã xuất hiện trong hộp màu xám :)

+0

cam kết edittingStyle làm việc cho tôi trong Swift 3 .. đăng một ví dụ làm việc lên .. của họ phương pháp workd cho tôi quá. nó có vẻ tốt hơn. – nyxee

1

cho swift 3

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     dataHandler.deletePeripheral(indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
     // - OR - 
     // mTableView.reloadData() //mTableView is an outlet for the tableView 
    } 
} 
Các vấn đề liên quan