2015-06-23 13 views
8

tôi thực hiện editActionsForRowAtIndexPathcommitEditingStyle swipe đang làm việc nhưng không có chỉnh sửa hành động xuất hiện trên UITableViewCelltôi thực hiện editActionsForRowAtIndexPath và commitEditingStyle nhưng không chỉnh sửa hành động xuất hiện trên tableViewCell khi vuốt sang các tế bào

thực hiện của tôi cho editActionsForRowAtIndexPathcommitEditingStyle như sau:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == UITableViewCellEditingStyle.Delete { 
      //I did some work here 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
     } 
    } 


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { 

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
     //I did some work here 
     tableView.reloadData() 
    }) 


    return [deleteAction] 
} 

Bất kỳ trợ giúp sẽ được đánh giá cao

+1

làm bạn canEditRowAtIndexPath impliemtn trở YES –

+0

vâng tôi đã làm và nó hoạt động trong một viewController mà không thực hiện canEditRowAtIndexPath – Abdelrahman

Trả lời

2

Bạn cần phải thực hiện canEditRowAtIndexPath từ Phương thức đại biểu UITableview và trả về true.

+0

Tôi đã thực hiện canEditRowAtIndexPath nhưng vẫn không làm việc – Abdelrahman

+0

vấn đề là editActions xuất hiện trong ViewController khác có và không triển khai canEditRowAtIndexPath nhưng trong ViewController khác chúng không xuất hiện nhưng thao tác vuốt hoạt động tốt – Abdelrahman

+0

Là bộ điều khiển khác một phân lớp của UIViewController hoặc một lớp con của UITableView Bộ điều khiển hoặc một số lớp khác mà bạn đang kế thừa chức năng? –

9

Tôi nghĩ bạn đã trộn lẫn hai loại chỉnh sửa khác nhau tại đây.

Loại chỉnh sửa đầu tiên là cũ UITableViewCellEditingStyle.Delete. Và cách mới là cung cấp chế độ xem phụ kiện tùy chỉnh của bạn.

Nếu bạn triển khai chế độ xem phụ kiện tùy chỉnh, thì các nút xóa mặc định sẽ không được hiển thị, do đó không được gọi. Vì vậy,

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

thậm chí có thể không được gọi, theo quan điểm của tôi.

Tài liệu của Apple Đối với editActionsForRowAtIndexPath chứa nội dung sau: If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row. Tôi giả định rằng nếu bạn thực hiện phương pháp này, chế độ xem phụ kiện chuẩn sẽ không được hiển thị.

Edit: Mã ví dụ (cập nhật để Swift 3 11/17/16)

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

private func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: IndexPath) -> [AnyObject]? { 
    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:IndexPath) -> Void in 
    }) 
    return [deleteAction] 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 

} 

Chỉnh sửa 2: Như rajagp chỉ ra, nếu bạn không cần thực hiện rỗng nếu bạn chỉ nhắm mục tiêu iOS9 (hoặc mới hơn).

+0

tôi thực hiện editActionsForRowAtIndexPath nhưng xem phụ kiện không được hiển thị – Abdelrahman

+0

nếu tôi không thực hiện func tableView (tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) thao tác vuốt không hoạt động – Abdelrahman

+1

Bạn cần triển khai trống để làm cho nó hoạt động. – gyan

0

Tôi vừa sao chép ViewController nơi các nút tác vụ hàng được hiển thị và hoạt động tốt và thay thế nút hành động hàng không xuất hiện khi trượt và giờ đây các nút tác vụ hàng được hiển thị và thực hiện hành vi mong muốn.

Nhưng tôi không hiểu vấn đề. không ai có thể giải thích điều đó?

1

Cố gắng thêm một số hành động nút bên xóa

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 
    { 
     let delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
      let alertView = UIAlertController(title: "Delete Action", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
      alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil) 

     }) 
     delete.backgroundColor = UIColor.redColor() 


     let more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
      let alertView = UIAlertController(title: "More Action", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
      alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil) 
     }) 
     more.backgroundColor = UIColor.blueColor() 

     return [delete, more] 
    } 
1

TLDR:

chức năng cần thiết để thực hiện: • commitEditingStyle • canEditRowAt • editActionsForRowAt

Lưu ý: cho editActionsForRowAt bạn có thể không quay trở lại một mảng trống ở đây - nó sẽ làm hỏng tất cả các ô. Nếu có một loại hàng cụ thể mà bạn không muốn cho phép chỉnh sửa hành động, hãy chỉ định điều này trong canEditRowAt để trả về false cho loại ô đó.

0

Xin lỗi vì thức dậy một chủ đề cũ như vậy, nhưng tôi đã nhận nó làm việc trên Swift 3 thực hiện:

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 
    return .delete 
} 
Các vấn đề liên quan