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 !
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()
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.
I am new to swift And programming. Please Anyone Help me for making call in IOS8 using UITabelViewCell left swipe not to delete A Row but to make a call in iPhone using Swift language
If you want to show a Delete or Insert button, you can use the following default functions of UITableView to accomplish that:
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Although if you want a different custom button, you should have a look at SWTableViewCell at this link: https://github.com/CEWendel/SWTableViewCell