How do I get the delete button to show when swiping on a UITableViewCell
? The event is never raised and the delete button never appears.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
Also, this can be achieved in SWIFT using the method as follows
Swift 3
All you have to do is enable these two functions:
This answer has been updated to Swift 3
I always think it is nice to have a very simple, self-contained example so that nothing is assumed when I am learning a new task. This answer is that for deleting
UITableView
rows. The project performs like this:This project is based on the UITableView example for Swift.
Add the Code
Create a new project and replace the ViewController.swift code with the following.
The single key method in the code above that enables row deletion is the last one. Here it is again for emphasis:
Storyboard
Add a
UITableView
to the View Controller in the storyboard. Use auto layout to pin the four sides of the table view to the edges of the View Controller. Control drag from the table view in the storyboard to the@IBOutlet var tableView: UITableView!
line in the code.Finished
That's all. You should be able to run your app now and delete rows by swiping left and tapping "Delete".
Variations
Change the "Delete" button text
Add the following method:
Custom button actions
Add the following method.
Note that this is only available from iOS 8. See this answer for more details.
Updated for iOS 11
Actions can be placed either leading or trailing the cell using methods added to the UITableViewDelegate API in iOS 11.
Further reading
Below UITableViewDataSource will help you for swipe delete
arrYears is a NSMutableArray and then reload the tableView
Swift
When you remove a cell of your tableview, you also have to remove your array object at index x.
I think you can remove it by using a swipe gesture. The table view will call the Delegate:
After removing the object. You have to reload the tableview use. Add the following line in your code:
after that, you have deleted the row successfully. And when you reload the view or adding data to the DataSource the object will not be there anymore.
For all other is the answer from Kurbz correct.
I only wanted to remind you that the delegate function won't be enough if you want to remove the object from the DataSource array.
I hope I have helped you out.
For Swift, Just write this code
For Objective C, Just write this code