I'm developing a small app using Swift, an UITableView to show custom cells, and then I would like to swipe to edit to do that I use the following code :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BasicCell
cell.Name.text = contacts[indexPath.row].Name
cell.Phone.text = contacts[indexPath.row].Phone
cell.ProfilPic.load(contacts[indexPath.row].ProfilPic!)
cell.ProfilPic.layer.masksToBounds = true
cell.ProfilPic.layer.cornerRadius = CGRectGetWidth(cell.ProfilPic.frame)/2.0
return cell
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?{
let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
print("delete")
}
let done = UITableViewRowAction(style: .Default, title: "Done") { action, index in
print("done")
}
return [delete, done]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
the problem is when I swipe a cell, buttons are not showing :
Related
I have this code and I need call a function when all buttons are show.
How can I tell when the animation that shows the editing actions has finished?'
My code has two button, this is the code:
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let confirm = UITableViewRowAction(style: .Normal, title: "Confirm") { action, index in
//CODE
}
confirm.backgroundColor = UIColor.greenColor()
let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
//code
}
delete.backgroundColor = UIColor.redColor()
return [delete,confirm]
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
I have tableView in the UIViewController class. I have to implement swipeable when to swipe it should show EDIT DELETE, that two action. My problem is, it's going inside the method when I swipe, but it did not show those two actions.
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .Default, title: "Edit") { action, index in
print("EDIT");
}
editAction.backgroundColor = UIColor.blueColor()
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
print("DELETE");
// self.confirmDelete();
}
deleteAction.backgroundColor = UIColor.redColor()
return [editAction,deleteAction]
}
I haven't implemented tableview controller. This tableview is part of my screen. Not full screen. I tried in the UITableViewController class, which was working
Any help please
Add this delegate method
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
In my app I have a tableView in witch I have default text and I would like to have a swipe to edit text function so the user can change the text if the want to.
I already added the swipe to delete, using the code below, however adding an editing text function isn't as easy to find information on, So my question is, How can I add a button so the user can edit the text via a swipe function?
I need to know the code to load the text as I can't have a textfield so it's not as easy as something like label.text = textfield.text
the code that original loads the text is as follows
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = places[indexPath.row]["name"]
Thanks !
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
places.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places")
tableView.reloadData()
}
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
var editAction = UITableViewRowAction(style: .Normal, title: "Edit") { (action, indexPath) in
tableView.editing = false
// your action
}
editAction.backgroundColor = UIColor.grayColor()
var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) in
tableView.editing = false
// your delete action
}
return [deleteAction, editAction]
I have custom UITableViewCell with some content on it. While I am trying to swipe (to show delete custom button with image) It's simply not working, or if I'll try to do that 10 times, so I could see the swipe delete button action.
my code is:
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MessagesCell
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: " ") { value in
// here some removing action
deleteAction.backgroundColor = UIColor(patternImage: UIImage(named: "swipeToDelete")!)
return [deleteAction]
}
This code will help
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
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)
}
}
Thank you.
The issue was in IIViewDeckController that blocked swipe with its PanGesture
I have the following swift code to implement a UITableViewRowAction, when I swipe a row the row slides out to the left as expected, however all the Section Header Rows also slide to the left with the table row at the same time.
I have also included a screen shot to show what is happening
If I remove the viewForHeader override and replace it with titleForHeaderInSection then I have no problem.
The reason for overiding the viewForHeader is that I want to place an image in the Header Row.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("header") as UITableViewCell
cell.textLabel?.text = instrumentGroups[section].name
cell.imageView?.image = UIImage(named: instrumentGroups[section].name)
return cell
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> InstrumentTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("instrumentCell", forIndexPath: indexPath) as InstrumentTableViewCell
cell.instrument = instrumentGroups[indexPath.section].instruments[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let instrument = instrumentGroups[indexPath.section].instruments[indexPath.row]
var actions: [AnyObject] = []
var action = UITableViewRowAction(style: .Normal, title: "Remove Watch") { (action, indexPath) -> Void in
tableView.editing = false
instrument.SetWatchList(false)
self.refresh()
}
action.backgroundColor = UIColor.redColor()
actions.append(action)
return actions
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
Simply return cell.contentView instead of cell within your viewForHeaderInSection function and your problem will be resolved.
I had the same problem. The answer is simple.
Because you use UITableViewCell as header view. Try UILabel or UITableViewHeaderFooterView instead.