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.
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,
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()
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 !
Basically to delete the cell "offline" I use this method so that whenever you swipe from right to left the user can delete the tableview cell.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
self.polls.remove(at: indexPath.row)
}
}
However, it obviously doesn't affect the CKRecord of the cell content I created before. So how can I fetch and delete the CKRecord data at the exact row the user swiped-to-delete on?
Assuming polls is the data source array declared as [CKRecord] you have to do three things.
Get the record from the data source array at the given index and remove it from the appropriate CKDatabase.
Remove the record from the data source array (you're already doing that).
Delete the row in the table view calling deleteRowsAtIndexPaths passing [indexPath].
For example (publicDatabase is the actual CKDatabase instance):
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let record = polls[indexPath.row]
publicDatabase.deleteRecordWithID(record.recordID, completionHandler: ({returnRecord, error in
// do error handling
})
polls.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
Edit:
For proper error handling you might have to put the code of step two and three into the completion block.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let record = polls[indexPath.row]
publicDatabase.deleteRecordWithID(record.recordID, completionHandler: ({returnRecord, error in
if error != nil {
// do error handling
} else {
self.polls.removeAtIndex(indexPath.row)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
})
}
}
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: