Am developing a app using swift in my app am using a tableview for adding contacts now i want to delete a row from the table view i have checked other stack overflow answer but nothing works out for me
code in my table view controller:
func
tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
functableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if (editingStyle == UITableViewCellEditingStyle.Delete)
{
dataArray.removeAtIndex(indexPath.row)
self.FirstTableView.reloadData()
}
}
This one not helps for me and cell not even moving and displaying delete button
Note:1.am using left navigation drawer so I cant swipe left side
2.I want to swipe in right side only
3.am not adding any constraints to anything am using size classes and auto layout
you check this delegate method you defined(I think you missed this)
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
}
and in cell commit style delete data like this:
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
context.deleteObject(dataarray[indexPath.row] as NSManagedObject)
dataarray.removeAtIndex(indexPath.row)
context.save(nil)
self.FirstTableView.reloadData()
Related
I'm trying to implement a simple TableView with Realm DataSource.
First, I create a ChangeSet
cars = realm.objects(CarModel.self)
carChangeset = Observable.changeset(from: cars, synchronousStart: false).observeOn(MainScheduler.instance)
and I created an instance of RxTableViewRealmDataSource
let dataSource = RxTableViewRealmDataSource<CarModel>(cellIdentifier: "Cell", cellType: CarListTableViewCell.self) { (cell, ip, element) in
cell.viewModel = CarDetailViewModel(car: element)
}
Then, I bound my changeset to the tableview
carChangeset
.bind(to: tableView.rx.realmChanges(dataSource))
.disposed(by: bag)
I run my project, but, no matter how I swipe a cell in my TableView, the "delete" button will never appear.
After looking for a solution to my problem, I read that, in order to a TableView to present the "red delete" button when swiping, the DataSource delegate must implement tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath). As RxTableViewRealmDataSource is not declared open on the original distribution, and I don't want to modify the sources, I created an extension that implements this function:
extension RxTableViewRealmDataSource where E: CarModel {
public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Should Delete car")
}
}
public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
}
Still nothing. My TableView does not react to the swipe gesture.
Is this a bug on the RxRealm/RxRealmDataSource implementation? Am I doing something wrong? Or do I missed something?
Thanks in advance,
how to automatically slide the cell when we touch button like adding contact? is there any delegate for that?
You should implement delegate method - commitEditingStyle and you will be done!!
something like,
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
// below code will delete one row - you can mange another kind of deletion here like delete from database also
if editingStyle == .Delete
{
yourDataArray.removeAtIndex(indexPath.row)
self.yourTableView.reloadData()
}
}
And you can do on button's click,
self.yourTableView.setEditing(true, animated: true)
to show (-) sign on every cells !
I added a custom swipe to share action on my UITableViewCells in my UITableView. My problem is that for some reason it won't even let me swipe the cell. It immediately crashes with this line:
reason: 'attempt to delete row 25 from section 0 which only contains 25 rows before the update'
However, I'm not trying to delete anything! It's a share button not a delete button. Here are the relevant methods I've added:
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action: UITableViewRowAction, indexPath: NSIndexPath) -> Void in
// Some stuff
}
shareAction.backgroundColor = UIColor.yellowColor()
return [shareAction]
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
Why is my table view controller giving me this error when it should have no business attempting to delete anything?
It turns out it was an issue unrelated to the three methods above. My table view controller is a PFQueryTableViewController subclass which has numberOfRowsInSection baked in out of the box. My problem was that I explicitly added that method when I shouldn't have. Therefore it was being called when it shouldn't have been (it's build into the subclass implicitly), resulting in some wonky behavior. I removed my call to numberOfRowsInSection and it worked as it should.
I am trying to learn Swift, but there is a problem in my project that drives me nuts.
I have a working list of data in a ViewController fed by parse.com. I managed to implement a swipe-feature that reveals buttons for both deleting and editing. That is working fine. Now I want the user to be able to reorder the cells. So I successfully implemented a button to put the table into editing-mode. My 2 problems with that are:
When I enter edit-mode I just want to be able to reorder the cells since editing and deleting is done via swipe (via "tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath)". How can I achive that the user doesn't see the 2 buttons for deleting and editing when in editing-mode and touching the delete-circle that is provided automatically?
Is it possible to remove the delete-circle altogether? Using "UITableViewCellEditingStyle.None" also disables the swipe-functionality.
Thanks in advance!
To avoid the round red delete button that appears when you put set UITableView isEditing to true at the left and does nothing when you click it, the minimum that worked for me was this (Swift 4, iOS 11)
// Avoid the round red delete button on the left of the cell:
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
I also have these functions, which probably interact:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return savedTempTable.isEditing
}
// Including this function in the delegate enable left-swipe deleting
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == .delete {
savedConversions.remove(at: indexPath.row)
}
}
// Including this function enables reordering
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath,to destinationIndexPath: IndexPath)
{
let elem = savedConversions.remove(at: sourceIndexPath.row)
savedConversions.insert(elem, at: destinationIndexPath.row)
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
Although people can delete a row through swipe the delete-button in editing mode should not be removed. People may not know about the swipe gesture and by removing the delete button (which they already expect in editing mode) the app becomes more difficult to use.
If you really want to remove the delete button then you have to implement the delegate method tableView(_:editingStyleForRowAtIndexPath:). There you can return .None while the screen is in editing mode and .Delete while the screen is not.
To enable reordering you have to implement the data source methods tableView(_:canMoveRowAtIndexPath:) and tableView(_:moveRowAtIndexPath:toIndexPath:).
You follow this way to remove the delete Icon while editing:
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellAccessoryNone;
}
My table view cell appears misaligned after swipe.
Here's tableView function:
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)
deleteItemFromCart(indexPath)
}
}
I am using custom cell which looks like this with constraints:
I had a look at this question, but it didn't help me.