tableView is not removing the last deleted cell immediately - ios

I'm having trouble removing the last cell I have tried tableView.reloadData() and beginUpdate(), endUpadate(), and all these methods works fine if I have more than one cell, but when I come to deleting the last cell it didn't work and the tableView won't reloadData at the same time that I delete the last cell, other cells 2-3-4 no problem, Plus when I close the app everyThing works fine.
im deleting the cells from the firebase store its a document that contains data if concern
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
self.DriverOffers.remove(at: indexPath.row)
self.DriverOrdersTV.deleteRows(at: [indexPath], with: .automatic)
self.DriverOrdersTV.reloadData()
}else if editingStyle == .insert {
self.DriverOrdersTV.beginUpdates()
self.DriverOrdersTV.insertRows(at: [IndexPath.init(row: 0, section: 0)], with: .automatic)
self.DriverOrdersTV.endUpdates()
}
}

Related

Delete rows from tableview using swift

I make a table view on my application, I want to add option that could the user delete the row in tableview.
I'm using this func
h1 is my array string that I put it in tableview.
my error is when I'm try to delete row in func editinstyle
var h1:[String] = ["one","two","three"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
h1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath as IndexPath) as! TableView1
cell.information.text = h1[indexPath.row]
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
h1.remove(at: indexPath.row) //Remove element from your array
print(h1)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
when I try to click to Delete it show like this 2020-02-11 20:43:35.803024+0200 TraniersApp[13314:401189] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Does someone know where is the problem?
To define "[indexPath]". It becomes [IndexPath(row: indexPath.row, section: 0)]
tableView.deleteRows(at: [IndexPath(row: indexPath.row, section: 0)], with: .automatic)
or
recall the particular section in the table view also
This will work outside of the function
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
Suspect this is a race condition that can be resolved by performing the h1.remove() and tableView.deleteRows() within a transaction (i.e., between tableView.beginUpdates() and tableView.endUpdates()).
tableView.beginUpdates()
h1.remove(at: indexPath.row) //Remove element from your array
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()

How to select another row right after deleting row in tableView (Swift)

I am deleting row and want to get next behavior:
If I am deleting row that was selected, I need to get another row selected automatically.
And also if I have an additional trouble, when I am perform deleting, target row that is going to be deleted selecting automatically, I don't need it to be selected as I want to only delete it.
The main question is 1 for now.
I understand that maybe row: 0 does not exists anymore but t's not a point now, I just simplified code to make it more comfortable to discuss the point. I tried performBatchUpdates, and tried to move selectRow(at:section:) to completion closure as well, row selection not works too.
selectRow works perfectly after insertRows function, so I am confused.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .middle)
}
}
As I understood your question, find this solution:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}
}
Pass indexPath while deleting a row and after endUpdates, you just need to set the same indexPath as SelectedRow.
Ok, I've found solution for the 1st question (just thought maybe such function can exist and typed didrowat, and xcode suggested this didEndEditingRowAt :)
override func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .middle)
}
Now first part of question is answered, I've inserted selection there.
The second question is still opened, how to make selected row keeping still selected while I am removing another row. I don't need selected row to deselect at all if I am removing another one.

Insert new row at the bottom upon deleting a row in UITableview

I have a requirement where I need to show a tableview with three rows always.
When user swipes to delete a row, I need to add a new row at the bottom.
While this happens, the swipe animation which moves the row to the left to delete has to be retained. I mean delete animation should not be affected. Is this possible?
First, your dataSource must return always 3 for your numberOfRows(in:) method. Then you can commit changes this way:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.performBatchUpdates({
// Delete the swiped row
tableView.deleteRows(at: [indexPath], with: .left)
// Get the last row index (numberOfRows-1)
let lastRowIndex = tableView.numberOfRows(inSection: indexPath.section)-1
// Insert a new row at the end
tableView.insertRows(at: [IndexPath(row: lastRowIndex, section: 0)], with: .top)
}, completion: nil)
}
}
Don't forget to update the rest of your dataSource accordingly, because cells may be reused. The code need to be added before call performBatchUpdates. Something like that:
var cellsText = ["A","B","C"]
// Inside your tableView(:commit:forRowAt) method:
if editingStyle == .delete {
cellsText.remove(at: indexPath.row)
cellsText.append("D")
// ...
}

Blank space between cells in a UITableView

I have a UITableView with several cells. If I do the swipe to delete (to remove a cell) it goes away but then I end up with blank space between my cells. It does not happen all the time. My cells are dequeued just fine. In the pic the green is my UITableView background and magenta is the UITableViewCell's content view. Where the space should be is where another cell should be; its just not drawing it. Using Xcode 9.2.
Any ideas as to why my cell isn't drawing?
My source for this VC is here:
https://github.com/bcgov/secure-image-ios/blob/master/SecureImage/UI/Albums/AlbumsViewController.swift
Make sure of this
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
self.catNames.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}

Deleting a key in Parse upon clicking "Delete" in table view cell

What I have is a table view with different cells that display different messages with other people you're chatting with. Upon swiping the cell to the left, a Delete option shows up. I want to be able to delete both the cell itself as well as the Room associated with it in Parse. This is what I have so far:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
//Delete messages here
let query = PFQuery(className: "Message")
query.whereKey("Room", equalTo: ??????)
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
tableView.endUpdates()
}
}
This function displays the Delete option, and when I click it, I want it to delete the cell it was clicked in. Does anyone know what goes inside the query? I have a Message class and I want to delete the Room key that's associated with the cell I delete.
Try this :
var dataToDelete:NSMutableArray = [] // this is a dummy array just to replicate your array
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
var object = dataToDelete[indexPath.row] as! PFObject
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
tableView.endUpdates()
object.delete()
}
}

Resources