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)
}
}
Related
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()
}
}
After successfully deleting a row from UITableView, I go to another view controller but when I return to the UITableView the deleted row is back again. Am I missing something? I am relatively new to Swift.
Here is the code:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
self.queuelayout.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print(self.queuelayout)
}
return [delete]
}
I've also tried with this code:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
queuelayout.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .right)
}
}
Look forward to your help.
I checked your top codes. They are all right. The only possible reason is as you said you went to another VC , you have already get rid of your tableViewController from the memory. Here is an simple example:
If your tableView is the most right one, and when you go back to middle vc, your right one will be removed from the memory. Then when you click assign button and actually, you are loading a completely new tableViewController.
As a result, viewDidLoad will be called and your tableViewController will init again to the original states.
You may set a breakpoint at func viewDidLoad(), to check this situation. If this one is not called when you go back, you are good to go. Otherwise, everything will be reset. All what you delete will reappear as they should.
This is a simple example. Your case may be a little complicated. But if your tableviewcontroller called ViewDidLoad(), you must have some hassles somewhere.
Hope this help you out.
I'm building an iOS app with a tableView, it's using a Realm database for the datamodel. When I try to delete a row by dragging the cell towards left, the section header follows the dragging movement to the left. When delete is pressed, the cell is deleted and the section header moves back into the right place.
Any clues to why the section header is moving with the cell?
The header cell is defined in the storyboard as a dynamic prototype cell, and the row cells are defined in a seperate xib and registered in the tableview. The section cell has the "Indent While Editing"-option unchecked in the storyboard.
The "Weekly report" is in the section header.
Here is the code I've implemented to enable editing:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
try! realm.write {
let reportToDelete = reportList[indexPath.row]
realm.delete(reportToDelete)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
}
I've tried both on the device and two different simulators, and with cleanin the build folder.
EDIT
The header is loaded from the storyboard, where it has a reuseable identifier: "WeeklyReportHeader", and the UIView in return by the tableView's delegate.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableCell(withIdentifier: "WeeklyReportHeader")! as UIView
}
I think this has something to do with using a cell as the header, probably some issue to do with cell reuse.
To fix this, rather than using the cell itself (and casting it as a UIView for the header), use it's contentView property, which is a UIView anyways.
Eg
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableCell(withIdentifier: "WeeklyReportHeader")!.contentView
}
I am running Xcode 6.4 developing in Swift.
I want to be able to swipe the cell to delete messages. This is the function I have:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
//Delete messages here
}
}
This is simply enabling the swipe function of the cells. However, I only see "Delet", without the last "e" at the end. Is this a bug?
Screenshot:
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.